我希望知道如何在不改变原始数值的情况下输出保留两位小数的数。
例如:
例如:
2229,999 -> 2229,99
我已经尝试过:
FORMAT(2229.999, 2)
CONVERT(2229.999, DECIMAL(4,2))
2229,999 -> 2229,99
我已经尝试过:
FORMAT(2229.999, 2)
CONVERT(2229.999, DECIMAL(4,2))
格式化数字到小数点后2位有两种选项:TRUNCATE
和ROUND
。你需要使用TRUNCATE
函数。
示例:
不进行四舍五入:
TRUNCATE(0.166, 2)
-- will be evaluated to 0.16
TRUNCATE(0.164, 2)
-- will be evaluated to 0.16
文档:https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_truncate
带有四舍五入:
ROUND(0.166, 2)
-- will be evaluated to 0.17
ROUND(0.164, 2)
-- will be evaluated to 0.16
文档:https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_round
ROUND
在处理货币数值时安全吗? - Renato Gama将2229.999
转换成保留两位小数的十进制数,可以使用以下代码:CAST(2229.999 AS DECIMAL(6,2))
convert(2229.999 as decimal(4,2))
。 - Pacerierconvert(2229.999, decimal(4,2))
。 - Pacerier只需使用
format(number, qtyDecimals)
sample: format(1000, 2)
result 1000.00
SELECT FORMAT(100000, 2);
的输出结果为:100,000.00
。我相信这会破坏一个期望得到数字的应用程序。 - But those new buttons though..CAST(vAvgMaterialUnitCost.`avgUnitCost` AS DECIMAL(11,2)) * woMaterials.`qtyUsed` AS materialCost
显示为十进制数 选择 ifnull(format(100.00, 1, 'en_US'), 0) 结果为 100.0
显示为百分数 选择 concat(ifnull(format(100.00, 0, 'en_US'), 0), '%') 结果为 100%
DECIMAL(6,2)
吗?其中 6 表示精度(即总位数),2 表示小数点后的位数。 - user5249203