Oracle 提供了以下四个函数用来做四舍五入。
CEIL 向上四舍五入
FLOOR 向下四舍五入
ROUND 四舍五入
TRUNC 去掉小数
下面让我们通过例子来看看它们之间的不同。oracle四舍五入
SELECT 5。5 NUM, CEIL(5。5) CEIL, FLOOR(5。5) FLOOR, ROUND(5。5) ROUND, TRUNC(5。5, 0) TRUNC FROM DUAL
UNION ALL
SELECT 2。5 NUM, CEIL(2。5) CEIL, FLOOR(2。5) FLOOR, ROUND(2。5) ROUND, TRUNC(2。5, 0) TRUNC FROM DUAL
UNION ALL
SELECT 1。6 NUM, CEIL(1。6) CEIL, FLOOR(1。6) FLOOR, ROUND(1。6) ROUND, TRUNC(1。6, 0) TRUNC FROM DUAL
UNION ALL
SELECT 1。1 NUM, CEIL(1。1) CEIL, FLOOR(1。1) FLOOR, ROUND(1。1) ROUND, TRUNC(1。1, 0) TRUNC FROM DUAL
UNION ALL
SELECT 1 NUM, CEIL(1) CEIL, FLOOR(1) FLOOR, ROUND(1) ROUND, TRUNC(1, 0) TRUNC FROM DUAL
UNION ALL
SELECT -1 NUM, CEIL(-1) CEIL, FLOOR(-1) FLOOR, ROUND(-1) ROUND, TRUNC(-1, 0) TRUNC FROM DUAL
UNION ALL
SELECT -1。1 NUM, CEIL(-1。1) CEIL, FLOOR(-1。1) FLOOR, ROUND(-1。1) ROUND, TRUNC(-1。1, 0) TRUNC FROM DUAL
UNION ALL
SELECT -1。6 NUM, CEIL(-1。6) CEIL, FLOOR(-1。6) FLOOR, ROUND(-1。6) ROUND, TRUNC(-1。6, 0) TRUNC FROM DUAL
UNION ALL
SELECT -2。5 NUM, CEIL(-2。5) CEIL, FLOOR(-2。5) FLOOR, ROUND(-2。5) ROUND, TRUNC(-2。5, 0) TRUNC FROM DUAL
UNION ALL
SELECT -5。5 NUM, CEIL(-5。5) CEIL, FLOOR(-5。5) FLOOR, ROUND(-5。5) ROUND, TRUNC(-5。5, 0) TRUNC FROM DUAL;
结果如下:oracle四舍五入
NUM CEIL FLOOR ROUND TRUNC
5。5 6 5 6 5
2。5 3 2 3 2
1。6 2 1 2 1
1。1 2 1 1 1
1 1 1 1 1
-1 -1 -1 -1 -1
-1。1 -1 -2 -1 -1
-1。6 -1 -2 -2 -1
-2。5 -2 -3 -3 -2
-5。5 -5 -6 -6 -5
取整(向下取整):oracle四舍五入
复制代码代码如下:
select floor(5。534) from dual;
select trunc(5。534) from dual;
上面两种用法都可以对数字5。534向下取整,结果为5。
如果要向上取整 ,得到结果为6,则应该用ceil
复制代码代码如下:
select ceil(5。534) from dual;
四舍五入:
复制代码代码如下:
SELECT round(5。534) FROM dual;
SELECT round(5。534,0) FROM dual;
SELECT round(5。534,1) FROM dual;
SELECT round(5。534,2) FROM dual;
结果分别为 6, 6, 5。5, 5。53
保留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 oracle四舍五入保留两位小数:http://www.youerw.com/fanwen/lunwen_99321.html