PHP : 2258574.18 not equals to 2058555.18 + 200019 - 0 [duplicate] - php

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;

Related

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

Replacing two Variables with each other's Value [duplicate]

This question already has answers here:
Is there a PHP function for swapping the values of two variables?
(20 answers)
Swap two variables value without using third variable in php [duplicate]
(2 answers)
Closed 4 years ago.
In this code
$x = 1;
$y = 2;
echo $x.$y; //12
I want to replace $x and $y vlaues with each other, So $x = 2 and $y = 1, Is that possible to be achieved without assigning middle-variables? Something like using a ready-function or like
$x = 1;
$y = 2;
echo $x.$y; //12
$x = &$y;
$y = &$x;
echo $x.$y //22
But instead of 22 I want to get 21.

after calculating percentage in php i got 87.555555 i wanna fixed it at 87.55 [duplicate]

This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 5 years ago.
<?php
var $a = 85;
var $b = 7;
var $c = $a/$b;
echo $c;
?>
Answer is 12.14285714285714
I want to fixed it as 12.14. How can we set two digits after decimal in PHP?
PHP doc : PHP Doc to round numbers
In your example :
var $b = 7;
var $c = $a/$b;
echo round($c, 2);
Hope it helps
You can use number_format:
number_format($c, 2)

PHP math divide it weird [duplicate]

This question already has an answer here:
PHP - Get rid of notation
(1 answer)
Closed 8 years ago.
I have
$a = $rowcount + $rowcount2 ; // this is 25
$b = 1000000000;
$total = $a / $b;
this gives me $total = 2.5E-8; but I want 0.000000025. What is causing this?
You need to tell PHP how to format your number
printf('%.9f', $a / $b);
or to store in a variable...
$num = sprintf('%.9f', $a / $b);

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