从数据库收到不必要的价格扩展


unnecessary price extension received from database

我的商品有不必要的价格长度。例如,如果价格是12.99,我将获得12.9899997711

我的数据库如下所示,有800多个项目

产品表

ppid         name             dec
1             shoes           black shoes
2             hat              red hat

项目_产品表

我的价格是type float(5,2)

Item_ID        ppid             price
1               1                12.99
2               2                10.00

PHP/HTML这就是我显示价格的方式

<?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>';
            }
        }
    ?>

摘要我如何才能让它显示末尾有.99.00的任何项目。

看起来您的价格字段可能被定义为某种浮点类型,这将无法准确地保存十进制数字。对于货币,您希望保存确切的金额,因此使用DECIMAL数据类型来定义列。