4 solutions to Oracle decimal point retention problems

Source: Internet
Author: User

The following article focuses on the actual retention of the Oracle decimal point. If you have requirements for extracting the Business list, we need to use the percentage. Normally, we need to keep two decimal places for it. In fact, we can only use round (_ data, 2), but the format is not neat.

If you do not have strict format requirements, you can use round. The following are the solutions you can find on the Internet:

Method 1: Use the fm format of to_char, that is:

 
 
  1. to_char(round(data.amount,2),'FM9999999999999999.00') as amount 

The disadvantage is that if the value is 0, it is displayed as. 00 instead of 0.00.

Note that the number of decimal points on the left side of Oracle in the format must be enough; otherwise, the number to be queried will be displayed as n symbols "#".

The solution is as follows:

 
 
  1. select decode(salary,0,'0.00',(to_char(round(salary,2),'fm99999999999999.00'))) from can_do; 

Method 2: Use case when then else end to determine and handle various situations:

 
 
  1. case  
  2. when instr(to_char(data.amount), '.') < 1 then  
  3. data.amount || '.00'  
  4. when instr(to_char(data.amount), '.') + 1 = length(data.amount) then  
  5. data.amount || '0'  
  6. else  
  7. to_char(round(data.amount, 2))  
  8. end as amount_format  

Method 3: You can use the parameter settings provided by Oracle, that is

 
 
  1. column amount format l9999999999.99 

The disadvantage of this method is that the number of 9 decimal points on the left of Oracle in the format must be known. Otherwise, the number exceeding the number is displayed.

Another problem is whether the setting takes effect at the session or system level when column is used.

Maybe the numeric column of a table does not always require that the data be displayed in the format of the last two decimal places in Oracle,

At this time, you can only use the session level, but there is a database connection session Timeout problem. If you do not use the system level, this method is not recommended.

Method 4: Use to_char + trim as follows:

 
 
  1. select trim(to_char(1234,'99999999999999.99')) from dual; 

Or

 
 
  1. select ltrim(trim(to_char(1234.525,'00000000000000.00')),'0') from dual; 

14 9 or 14 0 formats are used here. We recommend that you use 14 9 for convenience. The disadvantage of Method 4 is that

If the value is 0, it is converted to. 00 instead of 0.00. The remedy is to decode it.

Note that the number of decimal places on the left side of Oracle in the format of 9 or 0 is enough, and the number to be queried is displayed as n symbols "#".

As follows:

 
 
  1. select decode
    (salary,0,'0.00',trim(to_char(salary,'99999999999999.99'))) from can_do; 

Or

 
 
  1. select decode
    (salary,0,'0.00',ltrim(trim(to_char(salary,'00000000000000.00')),'0')) from can_do; 

CONCLUSION: The trim + to_char method in Method 4 is recommended or the method after method 1 is remedied. It is best to use the Oracle decimal point n 9, instead of 0, otherwise, trim processing is required.

That is:

 
 
  1. select decode(salary,0,'0.00',
    trim(to_char(salary,'99999999999999.99'))) from can_do; 

Or

 
 
  1. select decode(salary,0,'0.00',
    (to_char(round(salary,2),'fm99999999999999.00'))) from can_do;  

The above content is an introduction to the retention of decimal points in Oracle. I hope you will have some gains.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.