1

I am getting unnecessary price length from my item. for an example if the price is 12.99 I will get 12.9899997711

my database looks something like below with over 800 items

product table

ppid         name             dec
1             shoes           black shoes
2             hat              red hat

item_product table

my price is type float(5,2)

Item_ID        ppid             price
1               1                12.99
2               2                10.00

PHP/HTML This is how I show my price

<?php
        dbconnection(); 
        $stmt2 = $conn->prepare("SELECT name, Price FROM item_product WHERE ppid=:id LIMIT 1");
        $stmt2->bindParam('id',$id);
        $stmt2->execute();
        $rows2 = $stmt2->fetchAll(PDO::FETCH_ASSOC); 
        foreach ($rows2 as $row2) {
            if ($i == 0) {  
            echo '<td>Price:</td>';
                echo '<td name="pricetag" class="pricetag" id="pricetag">&pound;'.$row2['Price'].'</td>';
            }
        }
    ?>

Summary How can I get it to show .99 and .00 for any item that have any of those at the end.

Jessica
  • 61
  • 8

1 Answers1

6

It looks like your price field is probably defined as some sort of floating point type, which will not accurately save decimal numbers. For currencies, you want to save the exact amount, so use the DECIMAL data type to define the column.

Nick Coons
  • 3,682
  • 1
  • 19
  • 21