Search code examples
phppdosum

How to view SUM in price format?


I am using a PDO SUM statement to total the prices of items in a checkout page, I can get the correct result but if there is an item under 1.00 for example 0.70, it displays 0.7 instead of 0.70

So is there a way I can format it correctly? Thanks,

Here is my PHP...

<?php
 $res5 = $dbh->prepare("SELECT SUM(price) AS totPrice, username FROM checkout WHERE username = '$userdetails' AND cancelopt = ''");
 $res5->execute();
 $totalprice = $res5->fetch(PDO::FETCH_ASSOC);

 round($totalprice, 2);
?>


<?php echo $totalprice['totPrice']; ?>

Solution

  • as @KIKO Software said :

    try number_format

    <?php
    $res5 = $dbh->prepare("SELECT SUM(price) AS totPrice, username FROM checkout WHERE username = '$userdetails' AND cancelopt = ''");
    $res5->execute();
    $totalprice = $res5->fetch(PDO::FETCH_ASSOC);
    echo \number_format($totalprice['totPrice'], 2);
    ?>