Why is this outputting 87.5
and not 87.50
?
<?php
$quantity = 25;
switch ($quantity)
{
case ($quantity <= 50):
$price = 3.50;
break;
case ($quantity <= 100):
$price = 3.00;
break;
default:
break;
}
echo bcm开发者_运维知识库ul($price, $quantity, 2);
// 87.5
?>
It is rounding the 87.50 as 87.5 would be the same. To fix, you'd need:
number_format("87.50",2);
Use number_format()
instead of bcmul()
echo number_format(bcmul($price, $quantity, 2), 2, '.'); // forces to output always 2 diget after .
Mathmatically 87.5 is 87.50. If you need additional number padding, you can use number_format
or money_format
to display the extra 0
for php < 7.3 use
$val = bcmul('2', '5', 2);
$val = number_format($val, 2, '.', '');
// $val = "10.00"
or use php >= 7.3 it fixed
https://www.php.net/manual/en/function.bcmul.php#refsect1-function.bcmul-notes
精彩评论