-1

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.

code K.O.
  • 171
  • 1
  • 1
  • 14

1 Answers1

0

Try round() with precision 2

$e = $b+$c-$d;   
(round($e, 2) == round($a, 2) ) ? $x = "equals" : $x = "!equals"; 
echo $x;
ASR
  • 1,801
  • 5
  • 25
  • 33