PHP number minus same number not equal 0 [duplicate] - php

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);
}
}

Related

I want to found RHS = LHS in php [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Comparing floats - same number, but does not equal? [duplicate]
(3 answers)
Closed 2 years ago.
I want to check the given right hand side equation is equal to the left hand side or not. The value of $x = (19/30).
(((6*($x)) - 5)/(7 - (3*($x)))) = ((4 - (8 * $x))/((4 * $x) + 2))
I had tried below mentioned code, but it shown the RHS value is greater than LHS.
<?php
function check($m, $n)
{
$rhs = abs($m);
$lhs = abs($n);
if($rhs > $lhs){
echo "$m <strong style='color:red;'>is greater than</strong> $n";
}
elseif($rhs < $lhs) {
echo "$m <strong style='color:blue;'>is smaller than</strong> $n";
}
else{
echo "$m <strong style='color:green;'>is equal</strong> $n";
}
}
// $m = ((6*(19/30)) - 5)/(7 - (3*(19/30)));
// $n = (4 - (8 * (19/30)))/((4 * (19/30)) + 2);
$x = 19/30;
$m = abs(((6*($x)) - 5)/(7 - (3*($x))));
$n = abs((4 - (8 * $x))/((4 * $x) + 2));
check($m, $n);
?>
The output:
0.23529411764706 is greater than 0.23529411764706

cannot compare to float value in php [duplicate]

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;
?>

Can not get proper result for type casting in PHP [duplicate]

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

Removing trailing zero's from decimal [duplicate]

This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 8 years ago.
Hi i was wondering how i could remove the last 2 zero's as seen below:
13.6500
16.0000
17.5345
To the following:
13.65
16.00
17.5345
You can use round() like this to round to 2 decimal points like this
echo round("13.6500",2);
Also you can use number_format() like this
echo number_format("13.6500", 2);
Both yeilds the O/P
13.65
Demo
EDIT
from the comment i think what you want is like this
$input = "12.00000";
$length = strlen(substr(strrchr($input, "."), 1));
if($length > 2)
{
$input =$input+0;
if(strlen(substr(strrchr($input, "."), 1)) < 1)
{
$input = $input.".00";
}
else if(strlen(substr(strrchr($input, "."), 1)) < 2)
{
$input = $input.".0";
}
echo $input;
}
else
{
echo $input;
}
Demo

variables float no equals in php why? [duplicate]

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

Categories