This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I have got following code in php:
$canPrice = 4.98;
$insertedMoney = 5;
$remainingRest = $insertedMoney - $canPrice;
echo $remainingRest;
if( $remainingRest == 0.02 )
echo "equal";
else
echo "not equal".$remainingRest;
nothing more, it should print equal but somehow it's printing not equal, through I see, $remainingRest is equal 0.02.
Thanks for help in advance
Returns false because float values don't equal exactly when equalize.
You can try to equalize;
<?php
$canPrice = 4.98;
$insertedMoney = 5;
$remainingRest = $insertedMoney - $canPrice;
if(round($remainingRest,2) == round(0.02,2))
echo "equal";
else
echo "not equal".$remainingRest;
?>
Related
This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 2 years ago.
I have a $total with a value of 1400 which I'm trying to echo as 1,400.00 however sprintf and number_format refuse to cooperate with each other. How do I properly format the $total to echo as 1,400.00?
<?php
$total = 1400;
echo sprintf('%0.2f',$total);
//'1400.00'
echo number_format($total);
//'1,400'
echo sprintf('%0.2f',number_format($total_grand));
//'1.00'
echo number_format(sprintf('%0.2f',$total));
//'1,400'
?>
This should suffice:
$total = 1400;
echo number_format($total,2);
This will output:
1,400.00
See: number_format()
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
I have a problem with an if statement. The program doesn't go inside the if when it seems that the condition is true.
I tried to change the operator >= to == but it still doesn't work. It works with <= but I need >= and I also can't explain to myself why it's not working.
<?php
$balance = 0.03; // input 0.03
if($balance >= 0.02)
{
$returned = ((floor($balance / 0.02)) * 0.02); // 0.02
$balance = $balance - $returned; // 0.03 - 0.02 = 0.01 left
}
echo var_dump($balance) . PHP_EOL;
if($balance >= 0.01) // why doesnt go in ?
{
echo "Print Smt ....";
}
Why is the second if not true? Isn't 0.01 equal to 0.01?
Because $balance is reassigned value -1.47 which is less than 0.01.
This question already has answers here:
How to convert float value to integer in php?
(6 answers)
Is floating point math broken?
(31 answers)
Closed 4 years ago.
I have a Float variable
$float = 37.80
Then I multiply the float by 100:
$mul = $float *100
Now I have 3780.
$typecasting = (int) $mul;
Now when I print $typecasting, it shows 3779
For example:
$float = 37.80;
$mul = $float*100;
echo $mul;
echo '<br>';
//$mul = 3780
$typecasting = (int) $mul;
echo $typecasting;
// $typecasting shows 3779
I can't understand, how it works.
This problem occurs only for 32.80, 33.80, ...4.80. and also for multiples of 2, i.e. 65.60.
Just use floatval() http://php.net/manual/en/function.floatval.php
$float = 37.80;
$mul = $float*100;
echo $mul;
echo '<br>';
//$mul = 3780
$typecasting = floatval($mul);
echo $typecasting;
// $typecasting shows 3779
This question already has answers here:
In php, is 0 treated as empty?
(18 answers)
Closed 6 years ago.
I recently started writing a website for learning html/css/js/php.
I designed the front-end of my site with bootstrap.
Now I am trying to validate some inputs with PHP.
I tried this:
if ($durationHH <= 0 && $durationMM <= 0)
{
echo "DurationHH and DurationMM can not be both zero at the same time.";
echo "<br>";
echo "DurationHH and DurationMM can not be smaller than 0.";
}
elseif (empty($durationHH) || empty($durationMM))
{
echo "DurationHH and DurationMM can not be empty.";
echo "<br>";
}
else
{
echo $_POST["durationMM"];
echo ":";
echo $_POST["durationHH"];
}
I tested the validation by putting in some values for durationHH and durationMM.
Everything is working fine so far, except these two cases:
durationMM = 0 AND durationHH = any value
&&
durationHH= 0 AND durationMM = any value.
In these two cases i get the output: "DurationHH and DurationMM can not be empty."
How/why does this happen?
if durationHH and durationMM are always required to be integers or numeric, use ! is_numeric() to check.
This question already has answers here:
Compare floats in php
(17 answers)
Closed 9 years ago.
hello I have a problem wtich two variables in php. i have this code:
var_dump($total);echo '<br/>';
var_dump($reserva->getAdelanto());
if ($total == $reserva->getAdelanto()){
$total = 0;
echo "hello";
}
else
$total = $total - $reserva->getAdelanto();
print :
float(3940.2)
float(3940.2)
but does not enter the if when the two variables are equal. anyone knows why is that? greetings and thanks.
May be try with abs like
if ((abs($a)-abs($b)) <= 0.00001) {
echo "same";
}
Or
if (abs($a - $b) <= 0.00001) {
echo "same";
}
Or you also try like
var_dump( bccomp($a, $b) == 0 )
returns true if they are same