应用 number_format() 仅当数字已经千位分隔时
Apply number_format() only if not number already thousand separated
我有一个数字已经转换为千位分隔,但在全局模型中使用 number_format() 助手。因此,当已转换的数字收到 number_format 帮助程序时,我收到错误消息:
(1/1) ErrorException
A non well formed numeric value encountered
我正在尝试在 number_format() 助手之前检查模型函数,数字是否已经是千位格式。我不想修改现有代码,我只能在使用 number_format() 转换之前修改模型函数。 function/helper 可用吗?
你可以检查$value
如果是int
然后应用格式化,如果不是那么说明它已经格式化了,使用:
if(is_int($value))
{
$value = number_format($value, 2); // as an example here.
}
正如@Jerodev 在评论中提到的那样,您可能正在使用浮点数,因此 .
将被视为数字的一部分,但 ,
不会,因此如果您使用 ,
格式化数字 您可以使用 is_numeric
进行检查,如果数字已经格式化并且包含 ,
.
,它将 return 为假
我有一个数字已经转换为千位分隔,但在全局模型中使用 number_format() 助手。因此,当已转换的数字收到 number_format 帮助程序时,我收到错误消息:
(1/1) ErrorException A non well formed numeric value encountered
你可以检查$value
如果是int
然后应用格式化,如果不是那么说明它已经格式化了,使用:
if(is_int($value))
{
$value = number_format($value, 2); // as an example here.
}
正如@Jerodev 在评论中提到的那样,您可能正在使用浮点数,因此 .
将被视为数字的一部分,但 ,
不会,因此如果您使用 ,
格式化数字 您可以使用 is_numeric
进行检查,如果数字已经格式化并且包含 ,
.