1

您好,我有一个基本的 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 "";}
        }
4

2 回答 2

3

number_format

number_format($number, 2, '.', '');
于 2012-09-26T16:53:24.573 回答
0

回声时使用number_format

于 2012-09-26T16:53:28.600 回答