[英]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或4個參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.