1

I'm issuing a strange behavior with intval in PHP 7.0

It seems like using intval after a floating number computation returns wrong values.

Here's an example:

echo intval(920); // This prints 920 as expected
echo intval(9.2 * 100); // this prints 919!!!

Probably I'm misunderstanding the correct usage of intval.

Can someone explain me why this happens?

ProGM
  • 6,949
  • 4
  • 33
  • 52
  • refer to [php float issue](https://stackoverflow.com/q/21895756/6521116) – LF00 May 23 '17 at 09:10
  • 2
    This is because 9.2 is converted to a float. The float could have a value of 9.199999999999999999 which is lower than 9.2 because of limited precision. The integer part of 919.9999999999999 is 919. – KIKO Software May 23 '17 at 09:12

1 Answers1

3

Can you please try this:

(int)(9.2 * 10)

OR

See the example below, this is from a comment in the documentation

$n="19.99";
print intval($n*100); // prints 1998
print intval(strval($n*100)); // prints 1999
Zaeem
  • 382
  • 4
  • 16