1

I dont get it!

$a = intval("-945");
$b = floatval('9.45')*100;
$c = $a + $b;

var_dump($a);
var_dump($b);
var_dump($c);


$a = intval("-355");
$b = floatval('3.55')*100;
$c = $a + $b;

var_dump($a);
var_dump($b);
var_dump($c);

Results:

int(-945) float(945) float(-1.1368683772162E-13) int(-355) float(355) float(0)

Why not both c=0? Any ideas?

  • This looks like a possible duplicate of [Compare floats in php](https://stackoverflow.com/questions/3148937/compare-floats-in-php). Floating point numbers are not exactly precise, so a float number is never gaurenteed to be exactly a specific value. On the linked page, there are some good resources on why the error happens, and how to deal with it. – Thomas F Nov 27 '19 at 17:23

0 Answers0