Oracle database, numbers Force 2 decimal places to display

Source: Internet
Author: User
Tags postgresql
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 ;  
    3. 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


Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

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.