通过 number_format() 函数格式化后遇到格式不正确的数值
A non well formed numeric value encountered After Formatted by number_format() function
我正在使用 Accessor 来格式化我的数字值。当我尝试计算格式化值时问题开始,因为 number_format() 函数 return 一个字符串值。是否有任何替代方法来格式化数值并计算值。
class Order extends Model
{
protected $fillable = ['order_quantity'];
public function getOrderQuantityAttribute($value)
{
return number_format($value);
}
}
当我尝试计算时显示错误,
$order->order_quantity * 100;
//$order->order_quantity value is 10,000
只需将“,”替换为“”即可:
str_replace(',','',$order->order_quantity) *10
也许这会对您有所帮助。使用 str_replace
从字符串中删除逗号。
$newOrderQuantity = str_replace(',', '', $order->order_quantity); // converting to "10000"
以此类推,可以用(int)$newOrderQuantity
计算
我正在使用 Accessor 来格式化我的数字值。当我尝试计算格式化值时问题开始,因为 number_format() 函数 return 一个字符串值。是否有任何替代方法来格式化数值并计算值。
class Order extends Model
{
protected $fillable = ['order_quantity'];
public function getOrderQuantityAttribute($value)
{
return number_format($value);
}
}
当我尝试计算时显示错误,
$order->order_quantity * 100;
//$order->order_quantity value is 10,000
只需将“,”替换为“”即可:
str_replace(',','',$order->order_quantity) *10
也许这会对您有所帮助。使用 str_replace
从字符串中删除逗号。
$newOrderQuantity = str_replace(',', '', $order->order_quantity); // converting to "10000"
以此类推,可以用(int)$newOrderQuantity
计算