Several common examples of formatting numbers in JAVA

Source: Internet
Author: User

In JAVA, "data accuracy", such as 2.333333333333333333333, may occur during data computing. At this time, we have to consider formatting. I have summarized several issues, decimal point.

Note: As this is very simple, please refer to the Demo program below.

Format a number

In the NumberFormat class, four numeric formatting methods are provided: integer, decimal, currency, and percentage. The Factory method is getNumberInstance, getNumberIntance, getCurrencyInstance, the getPercentInstance method can be used to obtain the corresponding instance object. For example, we want to use a string to represent RMB 88888.88, so we can write it as follows:

NumberFormat nf = NumberFormat. getCurrencyInstance ();

System. out. println (nf. format (88888.88 ));

Customize and format numbers

However, NumberFormat cannot meet a slightly more complex requirement. Fortunately, java also provides DecimalFormat for custom formatting. To use a DecimalFormat object, you must provide it with a format (pattern ):

String pattern =...

DecimalFormat df = new DecimalFormat (pattern );

Or:

DecimalFormat df = new DecimalFormat ();

Df. applyPattern (pattern );

Then you can call its format method.

Example,

Directly run the code:

Import java. text. DecimalFormat;
Import java. text. NumberFormat;
Public class Test {
Public static void main (String [] ){
DecimalFormat df = (DecimalFormat) NumberFormat. getInstance ();
DecimalFormat df2 = (DecimalFormat) DecimalFormat. getInstance ();
DecimalFormat df1 = (DecimalFormat) DecimalFormat. getInstance ();
// Set the maximum number of decimal places to 2
Df. setMaximumFractionDigits (2 );
System. out. println ("sets the maximum number of digits to 2 decimal places ");
System. out. println ("12.1 after conversion ======" + df. format (12.1 ));
System. out. println ("12.1454651 after conversion ======" + df. format (12.1454651) + "\ n ");
// Set the number of decimal places to 2
Df1.applyPattern ("0.00 ");
System. out. println ("set the number of digits of the decimal point to 2 ");
System. out. println ("12.1 after conversion ======" + df1.format (12.1 ));
System. out. println ("12.1454651 after conversion ======" + df1.format (12.1454651) + "\ n ");
// Set the percentage
Df. applyPattern ("##. ##% ");
System. out. println ("set percentage ");
System. out. println ("1 after conversion ======" + df. format (1 ));
System. out. println ("0.016 after conversion ======" + df. format (0.016) + "\ n ");
// Set the group size
Df2.setGroupingSize (4 );
System. out. println ("set group size ");
System. out. println ("123456789 after conversion ======" + df2.format (123456789 ));
 }
}

Run the command. The result is as follows::

Sets the maximum number of decimal places to 2
12.1 after conversion ====== 12.1
12.1454651 after conversion ====== 12.15

Set the number of decimal places to 2
12.1 after conversion ====== 12.10
12.1454651 after conversion ====== 12.15

Set percentage
1 after conversion ====== 100%
0.016 after conversion ====== 1.6%

Set group size
123456789 after conversion ==== 6789

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.