Main class used: java.text.DecimalFormat
1. To instantiate an object, you can use the following two methods:
The code copy is as follows:
DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();
DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();
Because DecimalFormat inherits from NumberFormat.
2. Set the number of decimal places
The system defaults to the decimal places is 3, such as:
The code copy is as follows:
DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();
System.out.println(df.format(12.3456789));
Output: 12.346
Now you can set the decimal to two digits by the following method:
The code copy is as follows:
df.setMaximumFractionDigits(2);
System.out.println(df.format(12.3456789));
Then the output is: 12.35
3. There are two ways to convert numbers into percentage output:
(1)
The code copy is 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 outputs are: 1234.57% 100% 1.5%
(2)
The code copy is 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 outputs are:
1,234.57% 100% 1.5%
4. Set the group size
The code copy is 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:
The code copy is as follows:
DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();
df1.setGroupingSize(2);
df1.setGroupingUsed(false);
System.out.println(df1.format(123456789));
Output: 123456789
5. Set the decimal to 2 digits
The code copy is 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 format of the output numbers. How to use the Java class library to do this?
Maybe you don't care about the format, but you need to care about your program being universal around the world. A simple statement like the following is a region-dependent:
System.out.println(1234.56);
In the United States, "." is the decimal point, but it is not necessarily the case elsewhere. How to 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:
The code copy is 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, after running the program, 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 specific subclass of NumberFormat, such as DecimalFormat), which is suitable for formatting a number based on local settings. You can also use non-default regional settings, such as Germany. The formatting method then formats the numbers according to specific regional rules. This program can also be used in a simple form:
NumberFormat.getInstance().format(1234.56)
But saving a format and then reusing it is more efficient. Internationalization is a big problem when formatting numbers.
Another is effective control over the format, such as specifying the number of digits in the fractional part. Here is a simple example to solve this problem:
The code copy is 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 number format is set, using symbols like "####.000". This mode means that there are four numbers before the decimal point. If it is not enough, it will be empty. If there are three numbers after the decimal point, it will be filled with 0. Program output:
1234.560
1234,560
Similarly, formats in exponential form can also be controlled, for example:
The code copy is 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:
The code copy is 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 containing formatted numbers? Resolution support is included in NumberFormat. For example:
The code copy is 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, both of which parse a string: "1234,56". The first part uses local format to parse, while the second part uses German format to parse. When the program runs in the United States, the result is:
123456
1234.56
In other words, "1234,56" is considered a huge integer of 123456 in the United States, and a decimal of "1234.56" in Germany.
There is also the last question of formatting discussion. In the above example, both DecimalFormat and NumberFormat are used. DecimalFormat is often used to get good format control, while NumberFormat is often used to specify different regions than local. How to combine two categories?
The answer revolves around the fact that DecimalFormat is a subclass of NumberFormat, whose instance is specified as a specific region. Therefore, you can specify a region using NumberFormat.getInstance and then cast the structure to a DecimalFormat object. The documentation mentions that this technique can be applied in most cases, but you need to use a try/catch block to surround the cast in case the conversion does not work properly (presumably using a strange area in very unobtrusive situations). Here is an example like this:
The code copy is 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 mode, and outputs:
1234,56000
If you don't care about internationalization, you can use DecimalFormat directly.