Oracle number類型的格式化輸出

來源:互聯網
上載者:User

問題:

  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  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.