MySql格式化小数保留小数点后两位

方式一:

SELECT FORMAT(12562.6655,2);

结果:12,562.67

SELECT FORMAT(12332.1,4);

结果:12,332.1000(小数没有数字会补0)

查看文档:Formats the number X to a format like ‘#,###,###.##’, rounded to D decimal places, and returns the result as a string. If D is 0, the result has no decimal point or fractional part.整数部分超过三位的时候以逗号分割,并且返回的结果是string类型的。

方式二

select truncate(4545.1366,2);

结果:结果:4545.13(直接截取,不会四舍五入)

方式三

select convert(4545.1366,decimal(10,2));

结果:4545.14
convert()函数会对小数部分进行四舍五入操作,decimal(10,2):它表示最终得到的结果整数部分位数加上小数部分位数小于等于10,小数部分位数2

方式四

round:返回数字表达式并四舍五入为指定的长度或精度。

语法:ROUND ( numeric_expression , length [ , function ] )
参数:
numeric_expression :精确数字或近似数字数据类型类别的表达式(bit数据类型除外)。

length:是numeric_e-xpression 将要四舍五入的精度。length必须是tinyint、smallint或int。当length为正数时,numeric_e-xpression四舍五入为length所指定的小数位数。当length为负数时,numeric_e-
xpression则按length所指定的在小数点的左边四舍五入。

function:是要执行的操作类型。function必须是tinyint、smallint或int。如果省略function或function的值为0(默认),numeric_expression将四舍五入。当指定0以外的值时,将截断numeric_expression。

返回类型:返回与numeric_e-xpression相同的类型。
ROUND始终返回一个值。如果length是负数且大于小数点前的数字个数,ROUND将返回0。
示例

select ROUND(748.58, -4);

结果:0
当length是负数时,无论什么数据类型,ROUND都将返回一个四舍五入的numeric_e-xpression。
示例

ROUND(748.58, -1);
ROUND(748.58, -2);
ROUND(748.58, -3);

结果:
750
700
1000

select ROUND(4545.1366,2); 

结果:4545.15

猜你喜欢

转载自blog.csdn.net/strggle_bin/article/details/78060910