您好我有一个基本的PHP脚本,用于显示数组中一组项目的价格。该阵列有3条信息:名称,价格和sku。价格可以设置为99.95,12.95,10.50或仅5.00。到目前为止,一切似乎都运行良好,但当我尝试报告任何价格,如5.00或10.00(全元金额)时,它只是截断结束.00,这不是一个问题,但我宁愿让它显示尾随的零,所以一切看起来不错,类似。以下是阵列/报告循环的代码:
$items = Array
(
"0"=> Array
(
"name" => $_SESSION['itemname-mo'],
"price" => $_SESSION ['price-mo'],
"sku" => $_SESSION ['sku-mo']
),
"1" => Array
(
"name" => $_SESSION['itemname-addon'],
"price" => $_SESSION ['price-addon'],
"sku" => $_SESSION ['sku-addon']
),
"2" => Array
(
"name" => $_SESSION['itemname-addon1'],
"price" => $_SESSION ['price-addon1'],
"sku" => $_SESSION ['sku-addon1']
),
"3" => Array
(
"name" => $_SESSION['itemname-addon2'],
"price" => $_SESSION ['price-addon2'],
"sku" => $_SESSION ['sku-addon2']
)
);
$a_length = count($items);
for($x = 0; $x<$a_length; $x++){
$total +=$items[$x]['price'];
}
echo "<div class=\"well\">";
for($i = 0; $i < $a_length; $i++){
$name = $items[$i]['name'];
$price = $items[$i]['price'];
$sku = $items[$i]['sku'];
displaycart($name,$price,$sku);
}
echo "<br />
Sub Total:
$$total";
echo "</div>";
function displaycart($name,$price,$sku){
if($name != null || $price != null || $sku != null){
echo "$name: $$price ($sku) <br />";
}
else{ echo "";}
}
答案 0 :(得分:3)
请参阅number_format
。
number_format($number, 2, '.', '');
答案 1 :(得分:0)
回音时使用number_format
。