number_format() 导致错误“遇到格式不正确的数值”
我正在使用 number_format 将浮点数四舍五入为仅 2 位小数。问题是我的一些输入一开始的小数位数不超过 2 位。所以代码:
number_format($value, 2)
它不会在没有足够的十进制数字的情况下平静地添加 0,而是会在 Apache 日志中引发错误,这是不可取的。
因此 number_format(2.1, 2)
或 number_format(0, 2)
将在 Apache 日志中引发错误。
[Thu Jun 30 17:18:04 2011] [error] [client 127.0.0.1] PHP 注意:在线 /home/tahoang/Desktop/Projects/weatherData/weatherData.php 中遇到格式不正确的数值41
如何解决这个问题吗?
I am using number_format to round floats to only 2 decimal digits. The problem is that some of my inputs don't have more than 2 decimals digits to begin with. So the code:
number_format($value, 2)
Instead of peacefully adding 0 in case it doesn't have enough decimal digits, it raises errors inside Apache log and that's not desirable.
So number_format(2.1, 2)
or number_format(0, 2)
will raise error in Apache log.
[Thu Jun 30 17:18:04 2011] [error] [client 127.0.0.1] PHP Notice: A non well formed numeric value encountered in /home/tahoang/Desktop/Projects/weatherData/weatherData.php on line 41
How to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试将 number_format() 的第一个参数类型转换为 float:
或
Try type casting first parameter of number_format() to float:
or
尝试替换小数点,然后转换为浮点数。
无需更换:
Try to replace decimal point and after that cast to float.
Without replacing:
进行计算时,请使用
number_format($value, 2, ".", "")
如果您想显示末尾带有 .00 的数字(例如“50.50”而不是“50.5”)那么你可以使用
numer_format($value, 2)
When doing calculations, use
number_format($value, 2, ".", "")
And if you would like to display numbers with .00 at the end (like "50.50" not "50.5") then you would use
numer_format($value, 2)
我用过这个:
也许它会对某人有所帮助。
I used this:
Maybe it'll help someone out.
函数将其识别为字符串。
通过加零转换为数字:
Function is recognizing it as a string.
Convert to number by adding zero:
在
number_format()
函数中传递"0..6"
时出现此错误。我刚刚将表格的价格列中的
..
替换为.
。I got this error when passing
"0..6"
innumber_format()
function.I just replaced
..
to.
in price column of the table.我在这个错误上挣扎了很多,这是因为返回类型
float
像这样:错误不是
$number
而是字符串与“.”的转换。返回由number_format
生成的字符串时,浮点型中的“,”。I struggle a lot with this error and it was because a return type
float
like this:The error was not the
$number
but the conversion of string with "." and "," in float when returning the string generated bynumber_format
.