PHP float module not working

I wrote a function to add commas and zeros to a number, if necessary, but I'm stuck in a module function. According to my PHP:

float(877.5) % 1 == 0 //true 

Should not 877.5 % 1 == 0.5 ?

+8
floating-point php modulus
source share
3 answers

This gives you a reminder of what you need, fmod ,

fmod - returns the remainder with a floating point (modulo) of division of arguments

 echo fmod(877.5, 1); // 0.5 
+9
source share

No, the module operator tells you the rest of the division. Everything that is divided by 1 has no remainder, so it gives 0.

+1
source share

You can try your sample code from http://writecodeonline.com/php/

If you want to get only the decimal part, refer to What is the best way to get the fractional part of a float in PHP?

% will return only the remainder and everything that divides by 1, the reminder is always 0.

0
source share

All Articles