DecimalFormat class in Java

Source: Internet
Author: User

We often have to format numbers, such as 2 decimal places, which is the most common. Java provides the DecimalFormat class, which helps you to format numbers as quickly as you like. Here is an example:

  1. Importjava.text.DecimalFormat;
  2. publicclasstestnumberformat{
  3. Publicstaticvoidmain (String[]args) {
  4. doublepi=3.1415927; //PI
  5.     Take a single-digit integer
  6. System.out.println (Newdecimalformat ("0"). Format (pi)); //3
  7.     Take an integer and a two decimal place
  8. System.out.println (Newdecimalformat ("0.00"). Format (pi)); //3.14
  9.     Take two-bit integers and three decimal places, and 0 fill in the less-than-integer parts.
  10. System.out.println (new DecimalFormat ("00.000"). Format (pi)); 03.142
  11.     Take all of the integral parts of the
  12. System.out.println (Newdecimalformat ("#"). Format (pi)); //3
  13.     Count as a percentage and take two decimal places
  14. System.out.println (new DecimalFormat ("#.##%"). Format (pi)); //314.16%
  15. longc=299792458; //Speed of light
  16.     Display as scientific notation, and take five decimal places
  17. System.out.println (Newdecimalformat ("#.#### #E0"). Format (c)); //2.99792e8
  18.     Scientific notation shown as a two-bit integer, with four decimal places
  19. System.out.println (Newdecimalformat ("00.### #E0"). Format (c)); //29.9792e7
  20.     Each three bits is delimited by commas.
  21. System.out.println (Newdecimalformat (", # # #"). Format (c)); //299,792,458
  22.     Embed formatting into text
  23. System.out.println (Newdecimalformat ("The speed of light is the size of every second, # # #米.  "). Format (c));
  24. }
  25. }
  26. The DecimalFormat class mainly relies on # and 2 placeholder symbols to specify the number length. 0 indicates that if the number of bits is not sufficient, fill with 0, # indicates that the number is pulled up to this position whenever possible. The above example contains almost all the basic usage, if you want to learn more, please refer to the DecimalFormat class documentation.

DecimalFormat class in Java

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.