Laravel Blade 中的四舍五入数字

Rounding Off numbers in Laravel Blade

<td colspan="3">
    <p class="text-left">
        <h5>Discount ( @php echo "- $subtotalquant"; @endphp  )</h5>
    </p>
</td>
<td>
    <p class="text-right">
        <h5>
            @php
                echo"&#8369;$subtotal";
            @endphp
        </h5>
    </p>
</td>

我想将这些值四舍五入到小数点后两位。但是数字格式似乎不起作用。

看起来确实像这样^

$subtotal = 40.608

$subtotalquant = 10.152

像这样使用数字格式:

echo number_format((float)$subtotal, 2, '.', '');

echo number_format((float)$subtotalquant, 2, '.', '');

有关更多详细信息,您可以在此处查看文档:https://www.php.net/manual/en/function.number-format.php

{{ number_format($subtotal,2) }}
 {{ number_format($subtotalquant,2) }}

像这样使用,还使用花括号代替 echo。

{{round($subtotal, 2)}}

如果你想在 blade 文件中实现这段代码,那么使用它:

{{number_format((float)$subtotal, 2, '.', '')}}

{{number_format((float)$subtotalquant, 2, '.', '')}}

同样的事情,如果你想把它放在 PHP 文件中,那么请遵循 sagar 的代码。