number_format()导致错误“遇到格式错误的数值". [英] number_format() causes error "A non well formed numeric value encountered"
问题描述
我正在使用number_format将浮点数舍入到仅2个十进制数字.问题是我的某些输入开头没有超过2个小数位.所以代码:
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)
在没有足够的十进制数字的情况下,如果不是和平地加0,它将在Apache日志中引发错误,这是不希望的.
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.
因此number_format(2.1, 2)
或number_format(0, 2)
将在Apache日志中引发错误.
So number_format(2.1, 2)
or number_format(0, 2)
will raise error in Apache log.
[2011年6月30日星期四17:18:04] [错误] [客户端127.0.0.1] PHP注意:在线上的/home/tahoang/Desktop/Projects/weatherData/weatherData.php中遇到的格式不正确的数值41
[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
该如何解决?
推荐答案
尝试强制类型转换 number_format()浮动:
Try type casting first parameter of number_format() to float:
$format = number_format((float)0, 2);
或
$format = number_format(floatval(0), 2);
这篇关于number_format()导致错误“遇到格式错误的数值".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!