问题
I wrote a function to add commas and zeros to a number if necessary, but I've gotten stuck at the modulus function. According to my PHP:
float(877.5) % 1 == 0 //true
Shouldn't 877.5 % 1 == 0.5
?
回答1:
It's giving you the reminder of the division what you need is fmod,
fmod — Returns the floating point remainder (modulo) of the division of the arguments
echo fmod(877.5, 1); // 0.5
回答2:
No, the modulus operator tells you the remainder of the division. Anything divided by 1 does not have a remainder, so it yields 0.
回答3:
You may try your sample code from http://writecodeonline.com/php/
If you intend to get the decimal part alone, refer What's the best way to get the fractional part of a float in PHP?
% will return only the remainder and anything deivided by 1, reminder is always 0.
来源:https://stackoverflow.com/questions/23236851/php-float-modulus-not-working