1

Hello I have a basic php script which is used to display the price of a group of items in an array. The array has 3 pieces of information: name, price, and sku. The price could be set to something like 99.95, 12.95, 10.50 or just 5.00. Everything seems to work well so far, but when I try to report any price like 5.00 or 10.00 (whole dollar amounts) it just truncates the ending .00, which isnt exactly a problem but I would rather have it display the trailing zeros so everything looks nice and similar. Here is the code for the array/reporting loop:

$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 "";}
        }
Kynian
  • 660
  • 4
  • 15
  • 30

2 Answers2

3

See number_format.

number_format($number, 2, '.', '');
mbinette
  • 5,094
  • 3
  • 24
  • 32
0

Use number_format when echo'ing.

Ayush
  • 41,754
  • 51
  • 164
  • 239