PHP购物车计算

时间:2014-04-12 08:39:59

标签: php shopping-cart

您好我需要从购物车小计中删除10%

原始代码:

<?php echo number_format($order->subtotal,2);?>&OID=<?php echo $order->trans_id;?>

我知道这不准确,但是这样会有用吗?

<?php echo number_format($order->subtotal * 0.909090909,2);?>&OID=<?php echo $order->trans_id;?>

由于

3 个答案:

答案 0 :(得分:1)

使用sprintf()

$a =  2324.56*0.909090909 ;

echo sprintf('%0.2f',$a);

output // 2113.24

sprintf()将处理浮点进动,这是最好的处理方式。

如果需要显示特定区域设置的货币格式,可以使用money_format

$a =  2324.56*0.909090909 ;

$amount =  sprintf('%0.2f',$a);

setlocale(LC_MONETARY, 'en_US');

echo money_format('%(#1n', $amount) . "\n";
output // $2,113.24

以下是对number_format()-ve值进动问题

的解释

http://www.howtoforge.com/php_number_format_and_a_problem_with_negative_values_rounded_to_zero

答案 1 :(得分:0)

试试这个

$subtotal = $order->subtotal;
$cut_subtotal = $subtotal *(10/100);
$subtotal_new = $subtotal-$cut_subtotal;

现在在你的代码中使用它

<?php echo number_format($subtotal_new,2);?>

答案 2 :(得分:0)

这可能会起作用,但为什么不减去10%呢?如果这是目标,为什么不这样做呢?请记住number_format四舍五入,但我希望这是理想的。

<?php echo number_format( ($order->subtotal - ($order->subtotal* .1) ) ,2);?>

这是实际减去10%的数学,为什么不使用它而不是关闭的东西呢?

相关问题