当前位置:网站首页>Oracle database, numbers Force 2 decimal places to display-Alibaba Cloud

Oracle database, numbers Force 2 decimal places to display-Alibaba Cloud

2022-07-04 20:17:00 wx61ea6d592e17b

Here you could learn about oracle format number to 2 decimal places.

In the banking, financial and other sensitive to the digital requirements of the system, the digital display generally have strict requirements.

This is a requirement, title, which requires numbers to be displayed in two decimal places, and if there are no decimals, force the display to 0.

For example:

123.4 Display as 123.40

12 Display as 12.00

0 Display as 0.00

I thought this was a relatively simple question, and Oracle itself provided the TO_CHAR function, with the Format function, to satisfy the condition:

[SQL]

  1. Select To_char (123.4, ' 9999990.00 ') as AA from dual;
  2. Select To_char (n, ' 9999990.00 ') as AA from dual;
  3. Select To_char (0, ' 9999990.00 ') as AA from dual;
  4. Select To_char (123.4, ' fm9999990.00 ') as AA from dual;
  5. Select To_char (n, ' fm9999990.00 ') as AA from dual;
  6. Select To_char (0, ' fm9999990.00 ') as AA from dual;

At this point, I thought the problem was solved, but did not notice that the above statement is formatted with a string, not a number!!

requirements, very clear requirements, the number is formatted, the results are still numbers.

Analysis: This requirement is a very common and normal requirement, and since Oracle is so powerful, it should provide relevant methods,

So, to find the relevant documentation for Oracle, finally, find a cast function, which is responsible for the type conversion, try it.

The results show that the method is feasible. Test the SQL statement as follows:

[SQL]

  1. Select CAST (1234.4 as number (2)) as AA from dual;
  2. Select CAST ( as number (2)) as AA from dual;
  3. Select CAST (0 as number (2)) as AA from dual;

PS: Append a point, the string can be directly type conversion, without using the To_number () function to do intermediate conversion. The SQL statements are as follows:

[SQL]

  1. select cast ( ' 1234.4 '  as number  (10, 2)  )   as aa from dual ;  
  2. select cast ( ' 12 '  as number  (10, 2)  )  as aa  from dual ;  
    1. Li class= "alt" > select cast ( ' 0 '  as number  (10, 2)  )  as aa  from dual ;  

PS: Add 2nd, online to see if it is said that after the decimal point is full, PL/SQL version is concerned.

I did not do the verification, do not express personal opinion, only in this record, such as later encountered problems, and then verify

Oracle database, numbers Force 2 decimal places to display

​Related Article:​ ​how to round numbers to 2 decimal places




原网站

版权声明
本文为[wx61ea6d592e17b]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141131078598.html