Usage of the decimalformat Method for output numbers

Source: Internet
Author: User

Sometimes we need to control the format of the output number. How can we use the Java class library to achieve this?
Maybe you don't care about the format, but you need to care that your program can be used all over the world. A simple statement like the following is region-dependent:
System. Out. println (1234.56 );
In the United States, "." is a decimal point, but it is not necessarily in other places. How can this problem be solved?
Some packages in the Java. Text package can handle such problems. The following simple example uses the classes to solve the above problems:
Import java. Text. numberformat;
Import java. util. locale;
Public class decimalformat1 {
Public static void main (string ARGs []) {
// Obtain the local default format
Numberformat nf1 = numberformat. getinstance ();
System. Out. println (nf1.format (1234.56 ));
// Obtain the German format
Numberformat NF2 =
Numberformat. getinstance (locale. German );
System. Out. println (nf2.format (1234.56 ));
}
}
If you are in the United States, run the program and output:
1,234.56
1.234, 56
In other words, use different habits to represent numbers in different places.
The numberformat. getinstance () method returns an instance of numberformat (actually a subclass of numberformat, such as decimalformat), which is suitable for formatting a number based on local settings. You can also use non-default region settings, such as Germany. Then, the formatting method is used to format numbers according to specific region rules. This program can also use a simple form:
Numberformat. getinstance (). Format (1234.56)
However, it is more effective to save a format and reuse it. Internationalization is a big problem in formatting numbers.
The other is effective control of the format. For example, to specify the digits of the decimal part, the following is a simple example to solve this problem:
Import java. Text. decimalformat;
Import java. util. locale;
Public class decimalformat2 {
Public static void main (string ARGs []) {
// Obtain the local default format
Decimalformat DF1 = new decimalformat ("####. 000 ");
System. Out. println (df1.format (1234.56 ));
// Obtain the German format
Locale. setdefault (locale. German );
Decimalformat df2 = new decimalformat ("####. 000 ");
System. Out. println (df2.format (1234.56 ));
}
}
In this example, the digit format is set and the symbol like "####. 000" is used. This mode means that there are four digits before the decimal point. If it is not enough, there will be three digits after the decimal point. If it is not enough, it will be filled with 0. Program output:
1234.560
1234,560
Similarly, you can also control the exponential format, for example:
Import java. Text. decimalformat;
Public class decimalformat3 {
Public static void main (string ARGs []) {
Decimalformat df = new decimalformat ("0.000e0000 ");
System. Out. println (DF. Format (1234.56 ));
}
}
Output:
1.235e0003
For percentages:
Import java. Text. numberformat;
Public class decimalformat4 {
Public static void main (string ARGs []) {

Numberformat NF = numberformat. getpercentinstance ();
System. Out. println (NF. Format (0.47 ));
}
}
Output:
47%
Now you have seen several different techniques for formatting numbers. On the other hand, how do I read and parse strings containing formatted numbers? Resolution can be included in numberformat. For example:
Import java. util. locale;
Import java. Text. numberformat;
Import java. Text. parseexception;
Public class decimalformat5 {
Public static void main (string ARGs []) {
// Local format
Numberformat nf1 = numberformat. getinstance ();
Object obj1 = NULL;

// Format-based Parsing
Try {
Obj1 = nf1.parse ("1234,56 ");
}
Catch (parseexception E1 ){
System. Err. println (E1 );
}
System. Out. println (obj1 );
// German format
Numberformat NF2 =
Numberformat. getinstance (locale. German );

Object obj2 = NULL;
// Format-based Parsing
Try {
Obj2 = nf2.parse ("1234,56 ");
}
Catch (parseexception E2 ){
System. Err. println (E2 );
}
System. Out. println (obj2 );
}
}
This example is divided into two parts, both parsing a string: "1234,56 ". The first part uses the local format for parsing, and the second part uses the German format for parsing. When the program runs in the United States, the result is:
123456
1234.56
In other words, "1234,56" is regarded as a huge integer "123456" in the United States and "1234.56" in Germany ".
There is also the last question discussed in formatting. In the preceding example, both decimalformat and numberformat are used. Decimalformat is often used to obtain good format control, while numberformat is often used to specify different local regions. How to combine two classes?
The answer revolves around the fact that decimalformat is a subclass of numberformat and its instance is specified as a specific region. Therefore, you can use numberformat. getinstance to specify a region and forcibly convert the structure to a decimalformat object. This technology can be applied in most cases, however, you need to use try/Catch Block to enclose the forced conversion to prevent the conversion from working properly (probably using a strange region in a very unknown situation ). The following is an example:
Import java. Text. decimalformat;
Import java. Text. numberformat;
Import java. util. locale;
Public class decimalformat6 {
Public static void main (string ARGs []) {

Decimalformat df = NULL;
// Obtain a numberformat object and
// Forcibly convert to a decimalformat object
Try {
DF = (decimalformat)
Numberformat. getinstance (locale. German );
}
Catch (classcastexception e ){
System. Err. println (E );
}
// Set the format Mode
DF. applypattern ("####. 00000 ");
// Format a number
System. Out. println (DF. Format (1234.56 ));
}
}

Getinstance () method to obtain the format, then call applypattern () method to set the Format mode, output:
1234,56000
If you do not care about internationalization, you can directly use decimalformat.

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.