oracle 保留小數位

來源:互聯網
上載者:User

標籤:io   使用   ar   strong   for   資料   sp   問題   c   

方法一:使用to_char的fm格式 
to_char(round(data.amount,2),‘FM9999999999999999.00‘) as amount 
不足之處是,如果數值是0的話,會顯示為.00而不是0.00。 
另一需要注意的是,格式中小數點左邊9的個數要夠多,否則查詢的數字會顯示為n個符號“#”。 
解決方式如下: 
select decode(salary,0,‘0.00‘,(to_char(round(salary,2),‘fm99999999999999.00‘))) from can_do; 

方法二:使用case when then else end進行各種情況的判斷處理 
case 
when instr(to_char(data.amount), ‘.‘) < 1 then 
data.amount || ‘.00‘ 
when instr(to_char(data.amount), ‘.‘) + 1 = length(data.amount) then 
data.amount || ‘0‘ 
else 
to_char(round(data.amount, 2)) 
end as amount_format 

方法三:可以使用Oracle內建的參數設定 
column amount format l9999999999.99 
此方法的不足是,format中的小數點左面的9的個數要已知,否則會出現超過的數字顯示為########的情況。 
另外一個問題是,使用column時,設定生效是session級還是system級,需要注意。 
也許某張表的數值列不總是要求所有的地方顯示時,都是小數點後兩位的格式,此時只能使用session級,但是有個資料庫連接會話逾時的問題,如果不是使用到system級,不建議使用該方法。 

方法四:使用to_char+trim的方式 
select trim(to_char(1234,‘99999999999999.99‘)) from dual; 
或者 
select ltrim(trim(to_char(1234.525,‘00000000000000.00‘)),‘0‘) from dual; 
此處使用了14個9或者14個0的格式,建議使用14個9的方式,方便些。方法四的不足之處是: 
如果數值是0的話,轉化之後為.00而不是0.00,補救措施是,decode一下。 
另一需要注意的是,格式中小數點左邊9或者0的個數要夠多,負責查詢的數字會顯示為n個符號“#”。 
如下: 
select decode(salary,0,‘0.00‘,trim(to_char(salary,‘99999999999999.99‘))) from can_do; 
或者 
select decode(salary,0,‘0.00‘,ltrim(trim(to_char(salary,‘00000000000000.00‘)),‘0‘)) from can_do; 
結論:建議使用方法四中的trim+to_char的方式或者方法一的補救之後的方式,而且最好使用小數點左邊n個9的方式,不要使用0的方式,否則,要多一步trim處理。 
即:select decode(salary,0,‘0.00‘,trim(to_char(salary,‘99999999999999.99‘))) from can_do; 
或者 
select decode(salary,0,‘0.00‘,(to_char(round(salary,2),‘fm99999999999999.00‘))) from can_do;

oracle 保留小數位

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.