PHP float modulus not working

天大地大妈咪最大 提交于 2019-12-08 15:20:51

问题


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,

fmodReturns 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!