gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:09:07 26 4
gpt4 key购买 nike

我的商品的价格长度过长。例如,如果价格是 12.99,我将得到 12.9899997711

我的数据库看起来像下面有 800 多个项目

产品表

ppid         name             dec
1 shoes black shoes
2 hat red hat

item_product 表

我的价格是 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 数据类型来定义列。

关于php - 从数据库收到不必要的价格扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18040634/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com