Four Solutions to Oracle decimal point retention problems

Source: Internet
Author: User

If you need to retain the Oracle decimal point in practical applications, the actual method is very simple. We only use round (_ data, 2 )), however, if the format is not neat, you can use the round. The following is a solution for searching through the network:

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 digits on the left of the decimal point must be enough in the format. Otherwise, the number to be queried is 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 the format must be known; otherwise, the number exceeding the number is displayed.

Another Oracle decimal point retention problem is that when column is used, the setting takes effect at the session level or system level. Pay attention to this.

Maybe the numeric column of a table does not always require the display of all places, it is in the format of two digits after the decimal point,

At this time, you can only use the session level, but there is an Oracle decimal point reservation problem when the database connection session times out. If not, 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 digits to the left of the decimal point is enough in the format, 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 remedy method after method 1 is used, and it is best to use the method with n digits 9 on the left of the decimal point 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 Oracle decimal point retention issue. I hope you will get some benefits.

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.