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
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
17
There is the possibility to use the function fmod
(in English) which is appropriate for this. I don’t know if it will give the result you expect, but in my quick test it was ok.
Then you would:
echo fmod( 5.6, 3.0 );
This will print 2.6
.
Behold working in the ideone and in the repl it.. And put on the Github for future reference.
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.
Browser other questions tagged php mathematics float módulo division
You are not signed in. Login or sign up in order to post.