【问题标题】:How to fix 'A non well formed numeric value encountered' error in laravel?如何修复laravel中的“遇到格式不正确的数值”错误?
【发布时间】:2021-01-08 09:52:06
【问题描述】:

我今天无缘无故地收到了这个错误,因为我的 laravel 应用程序运行良好。

我的刀片中有这个价格代码:

<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>

但我有这个错误:

A non well formed numeric value encountered

在这一行:

return (int) $cart['product']->price * (int) $cart['quantity'];

在此之前,我的number_format 出现此错误,在搜索后,我发现我必须使用number_format((float)$price) 并解决了,但我不知道如何解决这个错误?!

【问题讨论】:

  • 如果你转储 $cart['product']-&gt;price$cart['quantity'] 变量,你会得到一个数字吗?在字符串或 int 中
  • @PatricNox 它是我的返回字符串
  • 你能告诉我们返回值吗?
  • string(8) "14750000", '14750000' 是价格
  • 这是只是变量之一的返回值吗?如果是这样,那么您会因为逗号而收到错误。

标签: php laravel laravel-8


【解决方案1】:

为什么不使用:

floatval(number_format($totalPrice)));

这将返回float

还有 return (int) $cart['product']-&gt;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']);

【讨论】:

  • 如果变量(我猜它确实如此)包含逗号或类似内容,仍然会失败。
  • 你如何得到一个逗号乘以 2 ints ?
  • 我有这个错误:调用未定义的函数 floatvalat()
  • 你拼错了floatval :)
  • 我仍然有非格式错误,错误属于这一行:return ((int) $cart['product']->price * (int) $cart['quantity']) + $post_price ;
【解决方案2】:

我发现了问题:))

代码没有任何问题,在管理面板中添加新的交付类型,在价格字段中我只需要写价格:20,而不是 20 美元

因为提交的价格只是整数,而不是整数和字符串:))))

感谢您的回答

【讨论】:

    猜你喜欢
    • 2012-08-11
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 2011-09-26
    • 2014-09-18
    相关资源
    最近更新 更多