如何將金額轉換為浮點數,並在PHP中進行正確地舍入?


要舍入金額,請在PHP中使用round()。假設我們的輸入值為以下內容 −

$amount=50.78;
$quantity=45.5;

像這樣將其轉換為浮點數 

$am=(float) round($amount, 4);
$quant=(float) round($quantity, 4);

示例

PHP程式碼如下所示

 線上演示

<!DOCTYPE html>
<html>
<body>
<?php
$amount=50.78;
$quantity=45.5;
$am=(float) round($amount, 4);
echo $am,"<br>";
$quant=(float) round($quantity, 4);
echo $quant,"<br>";
$correctAmount=round($am*$quant,4);
echo "The result is=",$correctAmount;
?>
</body>
</html>

輸出

這將產生以下輸出

50.78
45.5
The result is=2310.49

更新日期:2020年10月13日

194次瀏覽

開啟您的 事業

完成教程認證

開始
廣告
© . All rights reserved.