mysql 使用sum()小数点后出现很多位

数据库字段使用float类型,使用sum函数相加后出现后多小数位,原因是浮点数的不准确本质,它们无法以准确值保存在计算机体系结构中

方法一:最好的办法是将float字段改为decimal。具体方法:可设置一个临时字段,结合MySQL的关键字binary进行准确复制等。

方法二:使用binary关键字解决。具体操作为“select sum(binary 字段名(float类型))”

方法三:Cast。Cast(字段名 as 转换的类型 ),例如

SELECT 
count(id) AS total, 
cast( 
sum(commission) AS DECIMAL (19, 5) 
) AS sumComm 
FROM 
jd_settle_order 

方法四:使用round保留几位小数

如 round(201.42999449372,2)
 

你可能感兴趣的:(数据库)