【发布时间】:2012-07-20 00:14:06
【问题描述】:
我有一个 MySQL 表,即estimate_item,其中包含几列保存具有固定精度的十进制值。这是我的表结构。
表名:estimate_item
在使用 PHP 检索记录时,我使用 MySQL 的函数进行了一些基本计算,我使用的 SQL 查询是这样的。
SELECT
COUNT(ei.item_id) as total_item,
SUM(ei.quantity) as total_quantity,
SUM(ei.number_of_carton) as total_carton,
SUM(ei.number_of_carton * ei.cbm_per_carton) as total_cbm,
SUM(ei.quantity * ei.cost) as total_cost,
SUM(ei.quantity * ei.rate) as total_rate,
e.commission_amount,
SUM(ei.quantity * ei.rate) + e.commission_amount as total_amount
FROM
ai_estimate e
LEFT JOIN
ai_estimate_item ei ON ei.estimate_id = e.id
WHERE
ei.estimate_id = ?
例如上面的查询返回结果。
+------------+----------------+--------------+-----------+------------+------------+-------------------+--------------+
| total_item | total_quantity | total_carton | total_cbm | total_cost | total_rate | commission_amount | total_amount |
+------------+----------------+--------------+-----------+------------+------------+-------------------+--------------+
| 2 | 120 | 807 | 1122.6440 | 2.7500 | 137.5000 | 1500.00 | 1637.5000 |
+------------+----------------+--------------+-----------+------------+------------+-------------------+--------------+
由于以下列包含 4 个精度值total_cbm_total, total_cost, total_rate, total_amount,我想以这种方式对所有四列的值进行四舍五入。
a) 如果值为1122.6440,则将其四舍五入为1122.644
b) 如果值为2.7500,则将其四舍五入为2.75
c) 如果值为137.5000,则将其四舍五入为137.50
d) 如果值为1200.0000,则将其四舍五入为1200.00
即我希望这些值包含至少两个精度,根据值可以达到 3 或 4 个精度。
有什么方法可以直接在 MySQL 中执行此操作吗?还是 PHP 方式?
感谢和问候。
【问题讨论】:
-
根据您给出的示例,您要做的就是截断尾随零。如果你想在 PHP 中输出它们,你可以使用字符串操作或 number_format 函数。
-
如果我此时有值
1200.0000,则截断尾随零可能是一个问题,我想要两个尾随零,因此值应该是1200.00,不是吗?