原文地址:http://kb.cnblogs.com/a/1552790/
- SELECT ROUND( number, [ decimal_places ] ) FROM DUAL
说明:
number : 将要处理的数值
decimal_places : 四舍五入,小数取几位,不填默认为0
Sample :
- select round(123.456) from dual; 结果: 123
- select round(123.456, 0) from dual; 结果: 123
- select round(123.456, 1) from dual; 结果: 123.5
- select round(123.456, 2) from dual; 结果:123.46
- select round(123.456, 3) from dual; 结果: 123.456
- select round(-123.456, 2) from dual; 结果:-123.46
取整(向下取整):
- select floor(5.534) from dual;
- select trunc(5.534) from dual;
上面两种用法都可以对数字5.534向下取整,结果为5.
如果要向上取整 ,得到结果为6,则应该用ceil
- select ceil(5.534) from dual;
保留N位小数(不四舍五入):
- select trunc(5.534,0) from dual;
- select trunc(5.534,1) from dual;
- select trunc(5.534,2) from dual;
结果分别是 5,5.5,5.53,其中保留0位小数就相当于直接取整了。
数字格式化:
- select to_char(12345.123,'99999999.9999') from dual;
结果为12345.123
- select to_char(12345.123,'99999999.9900') from dual;
小数后第三第四为不足补0,结果为12345.1230
- select to_char(0.123,'99999999.9900') from dual;
- select to_char(0.123,'99999990.9900') from dual;
结果分别为 .123, 0.123