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
Related
This question already has answers here:
How do I safely perform money related calculations in PHP?
(3 answers)
Is floating point math broken?
(31 answers)
PHP - Floating Number Precision [duplicate]
(8 answers)
Closed last year.
I'm trying to minus 119.7 from 119.7 using PHP but the result is not expected (-1.4210854715202E-14), the expected result is 0
Doc1 RestToPay = 171
Doc2 RestToPay = 119.7
Here is my code:
$TotalAmountToPay = 290.7;
foreach($Docs as $Doc){
$RestToPay = $Doc['RestToPay'];
if($TotalAmountToPay <= 0){
break;
}
$TotalAmountToPay = $TotalAmountToPay - $RestToPay;
if($TotalAmountToPay >= 0){
echo "OK: ".$TotalAmountToPay;
}else{
echo "Done: ".$TotalAmountToPay;
}
}
Output I have got:
OK: 119.7
Done: -1.4210854715202E-14
It should help.
$TotalAmountToPay = 290.7;
foreach($Docs as $Doc){
$RestToPay = $Doc['RestToPay'];
if($TotalAmountToPay <= 0){
break;
}
$TotalAmountToPay = round($TotalAmountToPay,2) - round($RestToPay,2);
if($TotalAmountToPay >= 0){
echo "OK: ".$TotalAmountToPay;
}else{
echo "Done: ".$TotalAmountToPay;
}
}
It seems like some type of way those numbers are being stored and converted are not equal. You could always use the round() function which should get you what you want. So maybe try this:
$TotalAmountToPay = 290.7;
foreach($Docs as $Doc){
$RestToPay = $Doc['RestToPay'];
if($TotalAmountToPay <= 0){
break;
}
$TotalAmountToPay = $TotalAmountToPay - $RestToPay;
if($TotalAmountToPay >= 0){
echo "OK: ".round($TotalAmountToPay,4);
}else{
echo "Done: ".round($TotalAmountToPay,4);
}
}
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;
?>
This question already has answers here:
PHP: pass a variable to a function, do something to the variable, return it back
(5 answers)
Closed 3 years ago.
How come the output of these two functions are not the same? Please enlighten me on this one.
first code:
function multiply_itself(&$number) {
$number *= $number;
return $number;
}
$my_num = 10;
echo "$my_num" . "<br>"; //Output 10
multiply_itself($my_num);
echo "$my_num"; //Outputs 100
second code:
function doubled($integer) {
$integer *= $integer;
return $integer;
}
$integer = 20;
doubled($integer);
echo "$integer"; //Outputs 20, why not 400?
The second example outputs 20 because the parameter is not passed by reference like in the first example.
Change the function signature to function doubled(&$integer) or use its return value $integer = doubled($integer); and it prints 400.
This question already has answers here:
Compare floats in php
(17 answers)
Closed 6 years ago.
I'm struggling to make accounting software in PHP. And i got some bug from float precision.
Here is the sample unworking code :
$a = (float) 2258574.18;
$b = (float) 2058555.18;
$c = 200019;
$d = 0;
($b+$c-$d == $a ) ? $x = "equals" : $x = "!equals";
echo $x;
Outputs :
!equals
I was using round() abs() but doesnt solved.
It work if i only convert it to int (int).
I almost suicide because of this.
Try round() with precision 2
$e = $b+$c-$d;
(round($e, 2) == round($a, 2) ) ? $x = "equals" : $x = "!equals";
echo $x;
This question already has answers here:
How exactly does if($variable) work? [duplicate]
(8 answers)
Closed 7 years ago.
This is the code
<p>We are going to flip a coin until we get three heads in a row!</p>
<?php
$headCount = 0;
$flipCount = 0;
while ($headCount < 3) {
$flip = rand(0, 1);
$flipCount ++;
if ($flip) {
$headCount ++;
echo "<div class=\"coin\">H</div>";
} else {
$headCount = 0;
echo "<div class=\"coin\">T</div>";
}
}
echo "<p>It took {$flipCount} flips!</p>";
?>
I've played around with Java but I'm now learning PHP. The part that doesn't make sense right here is this.
if ($flip) {
$headCount ++;
echo "<div class=\"coin\">H</div>";
}
What exactly is being checked here? I doubt it's checking whether $flip is true.
It's a common shorthand in these kinds of languages. Once you get used to it, it seems natural.
In this case,
if ($flip)
is equivalent to:
if ($flip == 1)
But PHP is really just checking for a "truthy" value: true, not zero, not "0" or "", not [], and others will evaluate as true and pass the if ($flip) test.