DecimalFormat is a concrete subclass of NumberFormat, used to format decimal numbers. It can support localization of different types of numbers, including integers (123), fixed-point numbers (123.4), scientific notation-represented numbers (1.23E4), percentages (12%) and amounts ($123).
Let’s first introduce the usage of DecimalFormat:
import java.text.*; import java.util.*; public class DecimalFormatDemo { public static void main(String args[]) { DecimalFormat df1 = new DecimalFormat("###,###.0000");//Use the system default format System.out.println(df1.format(1111111123456.12)); Locale.setDefault(Locale.US); DecimalFormat df2= new DecimalFormat("##,###.0000");//Use the US format System.out.println(df2.format(1111111123456.12)); //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- (DecimalFormat)NumberFormat.getPercentInstance(); }catch(ClassCastException e){ <span style="white-space:pre"> </span> System.err.println(e); } df5.applyPattern("00.0000%"); System.out.println(df5.format(0.34567)); System.out.println(df5.format(1.34567)); } } (1) For rounding of data:
DecimalFormat contains a set of symbols, and the meaning of each symbol is explained as follows:
0 A number
# A number, not including 0
. Placeholder for decimal separator
, placeholder for group delimiter
; Delimited format.
- Default negative prefix.
% multiplied by 100 and displayed as percentage
? Multiply by 1000 and display as a kilogram; replace with currency symbols; if written double, replace with international currency symbols. If it appears in a pattern, use the currency decimal separator instead of the decimal separator.
X Prefix or any other character used in the suffix to refer to special characters in the prefix or suffix.
For example:
DecimalFormat df1 = new DecimalFormat("###0.00"); //Retain two decimal places, and if there are less than two decimal places, the zero will be automatically supplemented System.out.println(df1.format(124.367)); System.out.println(df1.format(124.3)); DecimalFormat df2 = new DecimalFormat("###0.##"); //Retain two decimal places, and if there are less than two decimal places, the zero will be supplemented System.out.println(df2.format(124.6)); System.out.println(df2.format(124)); DecimalFormat df3 = new DecimalFormat("000.000"); //Retain three decimal places, and make up for zeros where there are insufficient digits System.out.println(df3.format(24)); DecimalFormat df = new DecimalFormat("0.000E0000"); //Exponent System.out.println(df.format(1234.56)); DecimalFormat nf = (DecimalFormat)NumberFormat.getPercentInstance(); //PercentSystem.out.println(nf.format(0.476354)); nf.applyPattern("00.00%"); System.out.println(nf.format(0.476354)); Running results:
124.37 124.30 124.6 124 024.000 1.235E0003 48% 47.64%
(2) For reading and parsing strings 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); } } Running results:
123456 //Operation in the United States; considered string 1234.56 //Operation in Germany; considered a decimal
(3) For DecimalFormat and NumberFormat:
DecimalFormat is a subclass of NumberFormat, its 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).
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.