number_format() causes error “A non well formed numeric value encountered”

守給你的承諾、 提交于 2019-11-27 16:01:52

Try type casting first parameter of number_format() to float:

$format = number_format((float)0, 2);

or

$format = number_format(floatval(0), 2);

Try to replace decimal point and after that cast to float.

var_dump((float)number_format((float)str_replace(",", ".", "20,5"), 2, ".", ""));
result: float(20.5);

Without replacing:

var_dump((float)number_format(floatval("20,5"), 2, ".", ""));
result: float(20);
var_dump((float)number_format((float) "20,5", 2, ".", ""));
result: float(20);

I used this:

str_replace(array(".", ","), array(",", "."), $value)

Maybe it'll help someone out.

Function is recognizing it as a string.

Convert to number by adding zero:

number_format($NUMBER+0)

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)

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