Oracle removes 0 at the end of a decimal number

Source: Internet
Author: User

Oracle PL/SQL query statements sometimes convert the number type field to the varchar2 type
Reports or pages are frequently displayed:

. 440
. 441
1.0
10.100

It is very disturbing to say that the 0 before the decimal point is removed by the to_char or cast function, or the 0 at the end of the invalid decimal point is not removed.
The jsp interface is easy to handle. You can use a method similar to the following:
Jsp code

  1. Weight: <ww: text name ='Format. num.3'> <Ww: param value ='Person. weight'/> </Ww: text> (Km)
Weight: <ww: text name = 'format. num.3 '> <ww: param value = 'person. weight'/> </ww: text> (Km)

However, in a super report, the numbers to be displayed in cells are not very easy to process. You need to process the queried fields,
The following describes the solution:
Step 1: Save the 0 before the decimal point and remove the 0 at the end of the decimal point, and remove the redundant spaces generated by formatting:
Plsql code
  1. Select t. num from (
  2. Select ltrim (rtrim (to_char (0.44,'2014. 000'),'0'),'') As num from dual union
  3. Select ltrim (rtrim (to_char (0.441,'2014. 000'),'0'),'') As num from dual union
  4. Select ltrim (rtrim (to_char (1.0,'2014. 0'),'0'),'') As num from dual union
  5. Select ltrim (rtrim (to_char (10.100,'2014. 000'),'0'),'') As num from dual
  6. ) T
  7. /*
  8. Result:
  9. 0.44
  10. 0.441
  11. 1.
  12. 10.1
  13. */
Select t. num from (select ltrim (rtrim (to_char (0.44, '2017. 000 '), '0'), '') as num from dual union select ltrim (rtrim (to_char (0.441, '100. 00. 000 '), '0'), '') as num from dual union select ltrim (rtrim (to_char (1.0, '100. 00. 0 '), '0'), '') as num from dual union select ltrim (rtrim (to_char (10.100, '192. 000 '), '0'), '') as num from dual) t/* result: 0.2.16.4411.10.1 */


Step 2: Remove unnecessary decimal points:
Plsql code
  1. Select
  2. (Case when instr (t. num,'.') = Length (t. num) then rtrim (t. num,'.') Else t. num end) as num
  3. From (
  4. Select ltrim (rtrim (to_char (0.44,'2014. 000'),'0'),'') As num from dual union
  5. Select ltrim (rtrim (to_char (0.441,'2014. 000'),'0'),'') As num from dual union
  6. Select ltrim (rtrim (to_char (1.0,'2014. 0'),'0'),'') As num from dual union
  7. Select ltrim (rtrim (to_char (10.100,'2014. 000'),'0'),'') As num from dual
  8. ) T
  9. /*
  10. Result:
  11. 0.44
  12. 0.441
  13. 1
  14. 10.1
  15. */

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.