在PHP中计算TAX并显示正确的小数

时间:2014-01-08 16:20:55

标签: php decimal rounding

我正在尝试计算购物车的税金以及服务费。在大多数情况下,我认为我是正确的,但是,当我尝试输出总计时,如果数字以“0”结尾,则保持不变。换句话说,如果总计恰好是13.50美元,那么总计实际上显示为13.5美元

我正在回应信息,但这是我尝试过的代码。我目前正在使用ROUND。

            $tax = .065;
            $service = .18;
            $pricetotal = $price * $each_item['quantity'];
            $cartTotal = $pricetotal + $cartTotal;
            $totalwithtax = round($cartTotal + ($cartTotal * $tax), 2); //Order Items + Tax
            $servicecharge = round($totalwithtax * $service, 2); //service charge       
            $grandtotal = round($totalwithtax + ($totalwithtax * $service), 2); //service charge                        

非常感谢任何帮助。谢谢。

1 个答案:

答案 0 :(得分:3)

使用number_format()函数:)

http://www.php.net/manual/en/function.number-format.php

前:

echo number_format($grandtotal,2,'.','');
相关问题