简体   繁体   English

PHP浮点模数不起作用

[英]PHP float modulus not working

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:根据我的PHP:

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

Shouldn't 877.5 % 1 == 0.5 ?不应该877.5 % 1 == 0.5吗?

It's giving you the reminder of the division what you need is fmod , 它给你提醒你的部门你需要的是fmod

fmodReturns the floating point remainder (modulo) of the division of the arguments fmod - 返回参数除法的浮点余数(modulo)

echo fmod(877.5, 1); // 0.5

No, the modulus operator tells you the remainder of the division. 不,模数运算符会告诉您除法的其余部分。 Anything divided by 1 does not have a remainder, so it yields 0. 除以1的任何东西都没有余数,因此得到0。

You may try your sample code from http://writecodeonline.com/php/ 您可以尝试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? 如果你打算单独得到小数部分,请参考什么是在PHP中获取float的小数部分的最佳方法?

% will return only the remainder and anything deivided by 1, reminder is always 0. %将仅返回余数和任何除以1的任何值,提醒始终为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM