早上好,下面是一段代码,可以增加会话数组中每种产品的总成本。我的问题是,当最后一个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时如何显示/包含的任何建议?
答案 0 :(得分:0)
WebCode.ie 已在评论中回答了这个问题,但这是一个详细的答案,可能会帮助那些面临相同问题的人
要自定义数字格式,可以使用PHP函数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或4个参数。