繁体   English   中英

购物车计算总额

[英]Shopping cart calculating total

美好的一天,下面是一段代码,可以增加会话数组中每种产品的总成本。 我的问题是,当最后一个peny = 0时,总数的末尾显示0。

示例1,2.20 + 2.20 = 4.40,但仅显示4.4

示例2显示了2.20 + 2.25 = 4.45和4.45

$total = 0;
if (isset($_SESSION['cartItems'])){
    foreach ($_SESSION['cartItems'] as $product){
        $total += $product['cost'];
    }
}
echo $total;

关于输入0时如何显示/包含的任何建议?

WebCode.ie的评论已经回答了这个问题,但是这里有一个详细的答案,可以帮助面临相同问题的人们:

要自定义数字格式,可以使用PHP函数string number_format() ,该string number_format()可以通过以下方式接受一个两个四个 参数

$my_number=25200;    
$number_decimals=2;
$dec_separator=".";
$thousands_separator=" ";

echo number_format($my_number , $number_decimals, $dec_separator, $thousands_separator);

// This will output 25 200.00 
  1. 您要格式化的数字
  2. 小数位数
  3. 小数点分隔符
  4. 千位分隔符

另请注意,您只能使用1、2或4个参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM