The main use of the class: Java.text.DecimalFormat
1. To instantiate an object, you can use the following two methods:
Copy Code code as follows:
DecimalFormat df= (DecimalFormat) numberformat.getinstance ();
DecimalFormat df1= (DecimalFormat) decimalformat.getinstance ();
Because DecimalFormat inherits from NumberFormat.
2. To set the number of decimal places
The system default number of decimal places is 3, such as:
Copy Code code as follows:
DecimalFormat df= (DecimalFormat) numberformat.getinstance ();
System.out.println (Df.format (12.3456789));
Output: 12.346
You can now set the decimal number to two digits by using the following method:
Copy Code code as follows:
Df.setmaximumfractiondigits (2);
System.out.println (Df.format (12.3456789));
The output is: 12.35
3. Converting a number to a percentage output is like the next two ways:
(1)
Copy Code code as follows:
Df.applypattern ("##.##%");
System.out.println (Df.format (12.3456789));
System.out.println (Df.format (1));
System.out.println (Df.format (0.015));
The output is: 1234.57% 100% 1.5%
(2)
Copy Code code as follows:
Df.setmaximumfractiondigits (2);
System.out.println (Df.format (12.3456789*100) + "%");
System.out.println (Df.format (1*100) + "%");
System.out.println (Df.format (0.015*100) + "%");
The output is:
1,234.57% 100% 1.5%
4. Set Group size
Copy Code code as follows:
DecimalFormat df1= (DecimalFormat) decimalformat.getinstance ();
Df1.setgroupingsize (2);
System.out.println (Df1.format (123456789));
Output: 1,23,45,67,89
You can also disable grouping settings by df1.setgroupingused (false), such as:
Copy Code code as follows:
DecimalFormat df1= (DecimalFormat) decimalformat.getinstance ();
Df1.setgroupingsize (2);
Df1.setgroupingused (FALSE);
System.out.println (Df1.format (123456789));
Output: 123456789
5. Set decimal number to 2-bit
Copy Code code as follows:
DecimalFormat df2= (DecimalFormat) decimalformat.getinstance ();
Df2.applypattern ("0.00");
System.out.println (Df2.format (1.2));
Output: 1.20
Sometimes we need to control the output of the number of formats, how to use the Java class Library to do this?
Maybe you don't care about the format, but you need to be concerned that your program can be used around the world, as follows a simple statement that is dependent on the region:
System.out.println (1234.56);
In the United States, "." is a decimal point, but in other places it is not necessarily. How do you deal with this?
Some packages in the Java.text package can handle this type of problem. The following simple example uses those classes to solve the problem raised above:
Copy Code code as follows:
Import Java.text.NumberFormat;
Import Java.util.Locale;
public class DecimalFormat1 {
public static void Main (String args[]) {
Get the local default format
NumberFormat Nf1 = Numberformat.getinstance ();
System.out.println (Nf1.format (1234.56));
Get 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 after the 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 numberformat a specific subclass, such as DecimalFormat), which is suitable for formatting a number based on local settings. You can also use Non-default locale settings, such as Germany. The formatting method then formats the number according to the specific region rule. This program can also be used in a simple form:
Numberformat.getinstance (). Format (1234.56)
But saving a format and reusing it is more efficient. Internationalization is a big problem when formatting numbers.
Another is effective control over formatting, such as specifying the number of digits in a decimal part, and here is a simple example of how to solve this problem:
Copy Code code as follows:
Import Java.text.DecimalFormat;
Import Java.util.Locale;
public class DecimalFormat2 {
public static void Main (String args[]) {
Get the local default format
DecimalFormat df1 = new DecimalFormat ("####.000");
System.out.println (Df1.format (1234.56));
Get the German format
Locale.setdefault (Locale.german);
DecimalFormat DF2 = new DecimalFormat ("####.000");
System.out.println (Df2.format (1234.56));
}
}
In this example, the format of the number is set, using symbols like "####.000". This mode means that there are four digits before the decimal point, if not enough, there are three digits after the decimal point, not enough for 0. The output of the program:
1234.560
1234,560
Similarly, you can control the format of an exponential form, for example:
Copy Code code as follows:
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:
Copy Code code as follows:
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%
So far, you've seen several different techniques for formatting numbers. On the other hand, how do I read and parse a string that contains a formatted number? Parsing support is included in the NumberFormat. For example:
Copy Code code as follows:
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, all parsing a string: "1234,56". The first part uses local format parsing, and the second part uses German format parsing. When the program runs in the United States, the result is:
123456
1234.56
In other words, "1234,56" in the United States is considered to be a huge integer 123456, while in Germany it is considered a decimal "1234.56".
There is also the final question of formatting discussions. In the above example, both DecimalFormat and NumberFormat are used. DecimalFormat is often used to get good formatting control, whereas NumberFormat is often used to specify a different locale. How do you combine two classes?
The answer revolves around the fact that DecimalFormat is a subclass of NumberFormat, whose instance is designated as a specific region. Therefore, you can use Numberformat.getinstance to specify a region and then cast the struct to a DecimalFormat object. It is mentioned in the document that this technique can be used in most cases, but you need to surround the casts with try/catch blocks to prevent the conversion from working properly (presumably using a singular region in very unknown circumstances). Here is an example of this:
Copy Code code as follows:
Import Java.text.DecimalFormat;
Import Java.text.NumberFormat;
Import Java.util.Locale;
public class DecimalFormat6 {
public static void Main (String args[]) {
DecimalFormat df = null;
Get a NumberFormat object and
Cast to a DecimalFormat object
try {
DF = (DecimalFormat)
Numberformat.getinstance (Locale.german);
}
catch (ClassCastException e) {
System.err.println (e);
}
Set format mode
Df.applypattern ("####.00000");
Format a number
System.out.println (Df.format (1234.56));
}
}
The getinstance () method obtains the format and then calls the Applypattern () method to set the format pattern and output:
1234,56000
If you don't care about internationalization, you can use DecimalFormat directly.