1
$price = "19.65";
echo intval($price * 100); // 1964
echo  $price * 100; // 1965

I try to format the price amount, but I found the above solution have different result, any idea why?

what
  • 338
  • 1
  • 6
  • 13
  • Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – user3942918 Jul 13 '17 at 04:17

1 Answers1

2

Seems to be a floating point issue. This is from php.net manual page,

see http://php.net/manual/en/function.intval.php#101439

see http://php.net/manual/en/function.intval.php#112039

Do not use intval() when you really want round(). This is due to how PHP handles precision.

$n="19.99";
print intval($n*100); // prints 1998
print intval(strval($n*100)); // prints 1999
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • 1
    It doesn't explain why :-) – Capsule Jul 13 '17 at 03:16
  • Thanks for your quick response, I've come cross this on the manual site, but it doesn't explain much. I just bit of confuse, why it is 1998 rather than 1999 : ) – what Jul 13 '17 at 03:18