Oracle number类型的格式化输出

Linux大全评论1.6K views阅读模式

问题:

  1. 输入的浮点数与小数位,   
  2. input   output   
  3. ------- ---------   
  4.   0.123      0.12   
  5.    .123      0.12   
  6.      .1      0.10   
  7.    .199      0.20   
  8. 123.199  123.1990  
  1. create table test_t(   
  2.  col_1 number(3,2),   
  3.  col_2 number(7,4)   
  4.  );   
  5. insert into test_t(col_1) values(0.123);   
  6. insert into test_t(col_1) values(.123);   
  7. insert into test_t(col_1) values(.1);   
  8. insert into test_t(col_1) values(.199);   
  9. insert into test_t(col_2) values(123.199);   
  10. //   
  11. 1.直接查询数据,就能显示效果   
  12. x number(p,s):   
  13. p:x的有效位数,既x的宽度   
  14. s:x的小数位数   
  15. x的有效位数计算:   
  16. s>=0,x的有效位:p   
  17. s<0,x的有效位:p+|s|   
  18. select * from test_t;   
  19. COL_1     COL_2   
  20. ----- ---------   
  21.  0.12          /*因为小数点只是2位,在进行存储时进行了四舍五入*/  
  22.  0.12          /*因为小数点只是2位,在进行存储时进行了四舍五入,当整数部分空缺时,在数据最左边补齐一个0*/  
  23.  0.10          /*当实际值的小数位数小于指定位数时,在数字最右边补齐0*/  
  24.  0.20          /*存储时进行了四舍五入*/  
  25.        123.1990   
  26. //   
  27. 2.使用to_char()函数格式输出   
  28. SQL> select nvl(to_char(col_1,990.99),'Unknow') col_1,   
  29.   2         nvl(to_char(col_2,990.9999),'Unknow') col_2   
  30.   3         from test_t;   
  31. COL_1   COL_2   
  32. ------- ---------   
  33.    0.12 Unknow   
  34.    0.12 Unknow   
  35.    0.10 Unknow   
  36.    0.20 Unknow   
  37. Unknow   123.1990  

企鹅博客
  • 本文由 发表于 2020年9月13日 15:05:59
  • 转载请务必保留本文链接:https://www.qieseo.com/151686.html

发表评论