天天看点

Oracle - 数字处理 - 取上取整、向下取整、保留N位小数、四舍五入、数字格式化

1 用oracle sql对数字进行操作: 取上取整、向下取整、保留N位小数、四舍五入、数字格式化
 2 
 3 取整(向下取整): 
 4 select floor(5.534) from dual;
 5 select trunc(5.534) from dual;
 6 上面两种用法都可以对数字5.534向下取整,结果为5.
 7 
 8 如果要向上取整 ,得到结果为6,则应该用ceil
 9 select ceil(5.534) from dual;
10  
11 
12 四舍五入: 
13 SELECT round(5.534) FROM dual;
14 SELECT round(5.534,0) FROM dual;
15 SELECT round(5.534,1) FROM dual;
16 SELECT round(5.534,2) FROM dual;
17 结果分别为 6,  6,  5.5,  5.53
18  
19 
20 保留N位小数(不四舍五入)按位数截取: 
21 select trunc(5.534,0) from dual;
22 select trunc(5.534,1) from dual;
23 select trunc(5.534,2) from dual;
24 结果分别是 5,5.5,5.53,其中保留0位小数就相当于直接取整了。
25  
26 
27 数字格式化: 
28 select to_char(12345.123,'99999999.9999') from dual;
29 结果为12345.123
30 
31 select to_char(12345.123,'99999999.9900') from dual;
32 小数后第三第四为不足补0,结果为12345.1230
33 
34 select to_char(0.123,'99999999.9900') from dual;
35 select to_char(0.123,'99999990.9900') from dual;
36 结果分别为 .123, 0.123      

  作者:Jeremy.Wu

  出处:https://www.cnblogs.com/jeremywucnblog/

  本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。