Php 如何修复';遇到格式不正确的数值';拉威尔的错误?
今天我无缘无故地犯了这个错误,因为我的laravel应用程序运行良好 我的刀片中有我的价格代码:Php 如何修复';遇到格式不正确的数值';拉威尔的错误?,php,laravel,laravel-8,Php,Laravel,Laravel 8,今天我无缘无故地犯了这个错误,因为我的laravel应用程序运行良好 我的刀片中有我的价格代码: <li> @php $totalPrice = Cart::all()->sum(function ($cart){ return (int) $cart['product']->price * (int) $cart['quantity']; }) @endphp <span>T
<li>
@php
$totalPrice = Cart::all()->sum(function ($cart){
return (int) $cart['product']->price * (int) $cart['quantity'];
})
@endphp
<span>Total price : </span><span> {{ number_format((float)$totalPrice) }}</span>
</li>
在这方面:
return (int) $cart['product']->price * (int) $cart['quantity'];
在此之前,我的number\u格式
出现了这个错误,搜索之后,我找到了我必须使用number\u格式((float)$price)
的人,并解决了这个问题,但我不知道如何修复这个问题 为什么不使用:
floatval(number_format($totalPrice)));
它将返回float
和return(int)$cart['product']->price*(int)$cart['quantity']代码>
应该是:
return floatval($cart['product']->price * $cart['quantity']);
// or if you need that to be an integer:
return intval($cart['product']->price * $cart['quantity']);
我发现了问题:))
代码没有任何问题,在添加新交付类型的管理面板中,在价格字段中,我必须只写价格,如:20,而不是20美元
因为价格字段只是整数,而不是整数和字符串:))
感谢您的回答如果您转储$cart['product']->price
和$cart['quantity']]
变量,您会得到一个数字吗?或者是字符串或者int@PatricNox这是mecan的返回字符串,你给我们看返回值吗?字符串(8)“14750000”,“14750000”是价格是一个变量的返回值吗?如果是这样的话,那么您将得到由于逗号而导致的错误。如果变量(我猜它是这样的)包含逗号或类似的字符,那么仍然会失败。如何得到逗号乘以2int
s?我有一个错误:调用未定义的函数floatvalat(),您拼写错误了floatval
:)仍然存在格式不正确的错误,错误属于此行:返回((int)$cart['product']->price*(int)$cart['quantity'])+$post_price;
return floatval($cart['product']->price * $cart['quantity']);
// or if you need that to be an integer:
return intval($cart['product']->price * $cart['quantity']);