为什么输出87.5
而不是87.50
?
<?php
$quantity = 25;
switch ($quantity)
{
case ($quantity <= 50):
$price = 3.50;
break;
case ($quantity <= 100):
$price = 3.00;
break;
default:
break;
}
echo bcmul($price, $quantity, 2);
// 87.5
?>
它四舍五入87.50,因为87.5是相同的。要修复,您需要:
number_format("87.50",2);
使用number_format()
而不是bcmul()
echo number_format(bcmul($price, $quantity, 2), 2, '.'); // forces to output always 2 diget after .
数学87.5是87.50。如果需要额外的数字填充,可以使用number_format
或money_format
来显示额外的0
php<7.3使用
$val = bcmul('2', '5', 2);
$val = number_format($val, 2, '.', '');
// $val = "10.00"
或者使用php>=7.3它修复
https://www.php.net/manual/en/function.bcmul.php#refsect1-函数.cmul-notes