Java DecimalFormat: javadecimalformat
DecimalFormat is a subclass of NumberFormat used to format decimal numbers.
Package com. myk. hash; import java. text. decimalFormat; public class App {public static void main (String [] args) {// number format, # And 0 are placeholders ,#: when the number of digits to be formatted is insufficient, # blank space is filled; 0: when the number of digits to be formatted is insufficient, 0 DecimalFormat df1 = new DecimalFormat ("#. # "); // format the number System. out. println (df1.format (0.01); DecimalFormat df2 = new DecimalFormat ("0.000"); // number format System. out. println (df2.format (0.01); // The number is displayed in % format, and the decimal to percentage is automatically converted to DecimalFormat df3 = new DecimalFormat ("# 0.000%"); // number format System. out. println (df3.format (0.11); // The number is displayed in % format. The decimal to percentage is automatically converted to DecimalFormat df4 = new DecimalFormat ("#. ##% "); // format the number System. out. println (df4.format (0.01111); // use "," to separate digits by three digits. DecimalFormat df5 = new DecimalFormat (", ###"); // format the number in System. out. println (df5.format (981725025); DecimalFormat df6 = new DecimalFormat ("top, ###"); // number format System. out. println (df6.format (981725025); DecimalFormat df7 = new DecimalFormat ("###,###. 000 "); // format the number System. out. println (df7.format (981725025.981725025); // -- rounding }}
Result
0.010.01011.000% 1.11% 981,725,025 top 981,725,025 981,725,025.982
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.