本文共 530 字,大约阅读时间需要 1 分钟。
1.数值四舍五入,小数点后保留2位
round() 函数是四舍五入用,第一个参数是我们要被操作的数据,第二个参数是设置我们四舍五入之后小数点后显示几位。
numeric 函数的2个参数,第一个表示数据长度,第二个参数表示小数点后位数。
示例如下:
-- 4.56select cast(round(4.564,2) as numeric(5,2));-- 4.57select cast(round(4.565,2) as numeric(5,2));-- 4.57select cast(round(4.566,2) as numeric(5,2));-- 将 numeric 转换为数据类型 numeric 时出现算术溢出错误。1234.567 长度为7,大于5,故报错。select cast(round(1234.567,2) as numeric(5,2));-- 1234.57select cast(round(1234.567,2) as numeric(18,2));