Floats module in PHP returns integers?

Asked

Viewed 799 times

13

How do I get the rest of the division (operation module %) with decimal places when using a divisor or dividend float?

Example:

echo 5 % 3; // imprime 2 como é esperado
echo 5.6 % 3; // imprime 2 quando deveria imprimir 2.6

3 answers

17


8

The operator Module, server only for integers, for such use, you can use the function fmod of PHP:

fmod(5.6, 3); // imprime 2.6

If you only use the Module operator (%), the value obtained will be the Largest Integer Smaller than the result.

5

This operator only works with integers

There’s a comment on that link lembando this.

Note that the % operator (module) only works with integers (between -214748348 and 2147483647)...

Try using fmod()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.