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:
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:
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%
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:
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 in the United States "123456" and a decimal in Germany "1234.56".
DecimalFormat and NumberFormat connection
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:
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, output:
1234,56000
If you don't care about internationalization, you can use DecimalFormat directly.
where v is an unprocessed double, scale is the required precision, and returns a double that requires a decimal digit.
public static double round(double v,int scale){ if(scale<0){ throw new IllegalArgumentException( "The scale must be a positive integer or zero"); } BigDecimal b = new BigDecimal(Double.toString(v)); BigDecimal one = new BigDecimal("1"); return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleValue(); } The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.