How to get the closest multiple of 5 in PHP? - php

I need to round the returned number to closest multiple of 5 in this PHP.
return number_format(($entry['69']*$entry['68.2']*0.7), 0, ".", ",").'€' ;
How to get it correctly?

You can use round($val/5)*5:
$val = round($entry['69'] * $entry['68.2'] * 0.7 / 5) * 5;
return number_format($val, 0, ".", ",").'€' ;

Here is a custom function with greater detail of how everything is happening. You can change anything here if you need to update your code.
<?php
//Enter your code here, enjoy!
function roundClosest($num){
$helpVar = round($num/5);
$helpVar2 = $helpVar * 5;
if($num - $helpVar2 > 2.9){
$res = ($helpVar + 1) * 5;
}
else{
$res = $helpVar * 5;
}
return $res;
}
echo roundClosest(62.34);
echo "\n";
echo roundClosest(63.67);
Output:
60
65
Hope this helps!

Related

How to print factorial series?

I want to know how to print factorial in php. I have try to search on Google but unable to find result with solid prove example (Php Code). I find this forloop, i'm not satisfy with this.
for($c=3; $c>=1;$c--){
for($d=$c; $d>=1; $d--){
echo $c;
}// for ends
echo "<br />";
}// for ends
Out Put 333 22 1
I want this printed as output:
5 * 4 * 3 * 2 * 1
For calculating factorial,
function factorial($in) {
return array_product(range(1, $in));
}
and use it like,
echo factorial(5);
If you want to print factorial,
function factorial_print($in) {
return implode(' * ', array_reverse(range(1, $in)));
}
and use it like,
echo factorial_print(5);
Calculating a factorial is pretty straightforward, and doesn't need recursion either:
function factorial($x) {
$r = 1;
for ($i = 2; $i <= $x; ++$i) {
$r *= $i;
}
return $r;
}
echo factorial(10), PHP_EOL;
Try this one
function factorial($number) {
if ($number < 2) {
return 1;
} else {
return ($number * factorial($number-1));
}
}
Use following function inbuilt in PHP.
$fact1 = gmp_fact(5); // 5 * 4 * 3 * 2 * 1
echo gmp_strval($fact1) . "\n";
For more refer HERE

This same code not working in function. (generate EAN8)

I write a simple code to generate EAN8 code and check did record exist in database. When i use just code it's working perfect but when i try to change transform it for a function i have only blank page.
This code working
do{
$number = rand(1,9999999);
$number = str_pad($number, 7, 0, STR_PAD_LEFT);
$sum = ($number[0] * 3) +($number[1] * 1) + ($number[2] * 3) + ($number[3] * 1) + ($number[4] * 3) + ($number[5] * 1) + ($number[6] * 3);
$control = 10 - ($sum%10);
$ean8 = $number.$control;
$ean8 = substr($ean8, 0,8);
$db->where('numberCard',$ean8);
$user = $db->getOne('user');
}
while($db->count !== 0);
echo $ean8;
and this same code past in function return blank page
function generateEan(){
do{
$number = rand(1,9999999);
$number = str_pad($number, 7, 0, STR_PAD_LEFT);
$sum = ($number[0] * 3) +($number[1] * 1) + ($number[2] * 3) + ($number[3] * 1) + ($number[4] * 3) + ($number[5] * 1) + ($number[6] * 3);
$control = 10 - ($sum%10);
$ean8 = $number.$control;
$ean8 = substr($ean8, 0,8);
$db->where('numberCard',$ean8);
$user = $db->getOne('user');
}
while($db->count !== 0);
return $ean8;
}
echo generateEan();
Where is the problem ? Where i have made mistake? Please, help me and have a nice night
inside the scope of generateEan function, exists $db ?
maybe you can do a simple function and test if it exists

Why does converting Hex 0x00000000 to float give 5.8774717541114E-39 instead of 0.00 in PHP when used with a function?

I am using a function to convert hexadecimal numbers to floating point numbers. The function does not give me the right value for (0x00000000). I am using the following PHP function.
function hex2float($number) {
$binfinal = sprintf("%032b",hexdec($number));
$sign = substr($binfinal, 0, 1);
$exp = substr($binfinal, 1, 8);
$mantissa = "1".substr($binfinal, 9);
$mantissa = str_split($mantissa);
$exp = bindec($exp)-127;
$significand=0;
for ($i = 0; $i < 24; $i++) {
$significand += (1 / pow(2,$i))*$mantissa[$i];
}
return $significand * pow(2,$exp) * ($sign*-2+1);
}
Here is how I call the function
echo hex2float("0x00000000");
The output is 5.8774717541114E-39 which is wrong.
How do I get 0.00 from this function?
I have found a solution to the problem. Rounding off the return value of the function solved it for me.
return round($significand * pow(2,$exp) * ($sign*-2+1), 3);

PHP generate a random minus or plus percentage of a given value

I have a value, lets say its 1000.
Now I have to generate a random minus or plus percentage of 1000.
In particular I have to generate or a -20% of 1000 or a +20% of 1000 randomly.
I tried using rand() and abs() but with no success..
Is there a way in PHP to achieve the above?
A bit of basic mathematics
$number = 1000;
$below = -20;
$above = 20;
$random = mt_rand(
(integer) $number - ($number * (abs($below) / 100)),
(integer) $number + ($number * ($above / 100))
);
rand(0, 1) seems to work fine for me. Maybe you should make sure your percentage is in decimal format.
<?php
$val = 10000;
$pc = 0.2;
$result = $val * $pc;
if(rand(0, 1)) echo $result; else echo -$result;
if(rand(0, 1)) echo $result; else echo -$result;
if(rand(0, 1)) echo $result; else echo -$result;
if(rand(0, 1)) echo $result; else echo -$result;
if(rand(0, 1)) echo $result; else echo -$result;
?>
$number = 10000;
$percent = $number*0.20;
$result = (rand(0,$percent)*(rand(0,1)*2-1));
echo $result;
Or if you want some sort of running balance type thing....
function plusminus($bank){
$percent = $bank*0.20;
$random = (rand(0,$percent)*(rand(0,1)*2-1));
return $bank + $random;
}
$new = plusminus(10000);
$new = plusminus($new);
echo $new."<br>";
$new = plusminus($new);
echo $new."<br>";
$new = plusminus($new);
echo $new."<br>";
$new = plusminus($new);
echo $new."<br>";
$new = plusminus($new);
echo $new."<br>";
$new = plusminus($new);
I know this is really old now but stumbled across it looking for something similar where I needed a random sign (+ or -) so opted for a random boolean:
<?php $sign = (rand(0,1) == 1) ? '+' : '-'; ?>
Thanks to this this answer.
So I would opt for a solution like this:
<?php
// Alter these as needed
$number = 1000;
$percentage = 20;
// Calculate the change
$change_by = $number * ($percentage / 100);
// Set a boolean at random
$random_boolean = rand(0,1) == 1;
// Calculate the result where we are using plus if true or minus if false
$result = ($random_boolean) ? $number + $change_by : $number - $change_by;
// Will output either 1200 or 800 using these numbers as an example
echo $result;
?>

PHP MySQL read from a table to use in a graph

I have a graph that auto fills based on a few values. The values are the goal (total) the current (where it is at now) and in the total height of the graph. I am trying to pull the goal and current out of the database format it into money and it should fill the graph to the point it needs to be.
I had it working with a URL get which was ?current=584559096&goal=1000000000 and I just used $_GET. For easier maintenance for others around I want to pull it from a database which can be updated a post.
The code I have so far is:
<? php function formatMoney($number, $fractional=false) {
if ($fractional) {
$number = sprintf('%.0f', $number);
}
while (true) {
$replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number);
if ($replaced != $number) {
$number = $replaced;
} else {
break;
}
}
return $number;
}
$goal = $row['goal'];
$current = $row['current'];
$percent = round($current / $goal * 100);
$totalheight = 216;
$ourheight = $totalheight * $percent / 100;
$halfofarrowheight = 12;
$arrowheight = $totalheight-$ourheight-$halfofarrowheight;
if($arrowheight < 0)
$arrowheight = 0;
mysql_close($con);
?>
Here is my query
mysql_select_db("databasename", $con);
$result = mysql_query("select * from `dbname`.`tablename");
This is my error Warning: Division by zero in E:\inetpub\wwwroot\test.website.net\web\includes\globalfoot.php on line 36
Line 36 is the $percent = round($current / $goal * 100);
So, I been fooling with this thing for 4 days now trying to just get the numbers out of the goal column and current column format them to money and have the chart fill. For test sake lets say the goal is 1000000000000 and the current is 5000000000.
$row['goal'] = $goal;
$row['current'] = $current;
Please forgive me if I haven't understood your code, but shouldn't the above be:
$goal=$row['goal'];
$current=$row['current'];
Your code should look like this:
mysql_select_db("databasename", $con);
$result = mysql_query("select * from `dbname`.`tablename");
if (!$result || !($row = mysql_fetch_assoc($result))) {
echo "DB error: ".mysql_error();
exit;
}
$goal = $row['goal'];
$current = $row['current'];
// ...
Always, always double-check your variables when doing division.
$percent = ($goal > 0 || $goal < 0) ? round($current / $goal * 100) : 0;

Categories