ROUND函数

ROUND函数是四舍五入函数,返回数字表达式并四舍五入为指定的长度或精度。
语法:
ROUND ( numeric_expression , length [ , function ] )
参数:
numeric_expression
精确数字或近似数字数据类型类别的表达式(bit 数据类型除外)。
length
是 numeric_expression 将要四舍五入的精度。length 必须是 tinyint、smallint 或int。当 length 为正数时,numeric_expression 四舍五入为 length 所指定的小数位数。当 length 为负数时,numeric_expression 则按 length 所指定的在小数点的左边四舍五入。
function
是要执行的操作类型。function 必须是 tinyint、smallint 或 int。如果省略 function 或 function 的值为 0(默认),numeric_expression 将四舍五入。当指定 0 以外的值时,将截断 numeric_expression。
返回类型
返回与 numeric_expression 相同的类型。
注释
ROUND 始终返回一个值。如果 length 是负数且大于小数点前的数字个数,ROUND 将返回 0。

示例

Result

 Remark

select round(748.58,-1)

750.00

从小数点左边第1位四舍五入,其他位为0

select round(748.58,-2)

700.00

从小数点左边第2位四舍五入,其他位为0

select round(748.58,-3)

1000.00

从小数点左边第3位四舍五入,其他位为0

select round(748.58,-4)

0

length 是负数且大于小数点前的数字个数,ROUND 将返回 0

select round(748.58,1)

748.60

四舍五入为length所指定的小数位数

select round(748.58,2)

748.58

四舍五入为length所指定的小数位数

select round(124.9994,3)

124.9990

四舍五入为length所指定的小数位数

select round(124.9995,3)

125.0000

四舍五入为length所指定的小数位数

select round(150.75,0)

151.00

四舍五入的结果

select round(150.75,0,1)

150.00

截断的结果

原文地址:https://www.cnblogs.com/perfect/p/556374.html