PHP浮点模数不工作 [英] PHP float modulus not working

查看:138
本文介绍了PHP浮点模数不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果需要,我写了一个函数给一个数字添加逗号和零,但是我已经卡住了模数函数。根据我的PHP:

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

不应该 877.5%1 == 0.5

解决方案

它给你提醒你需要的是 fmod


$ b fmod - 返回浮点数参数划分的余数(模)

  echo fmod(877.5,1); // 0.5 


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?

解决方案

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

这篇关于PHP浮点模数不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆