Java. text. DecimalFormat class decimal format,
The specific code of the java. text. DecimalFormat class in decimal format is for your reference. The specific content is as follows:
Import java. text. decimalFormat; class FormatDemo {public void format1 (String pattern, double value) {// declare a DecimalFormat object DecimalFormat df = new DecimalFormat (pattern); String str = df. format (value); System. out. println ("format with" + pattern + ":" + value + "Get:" + str) ;}} public class Test {public static void main (String args []) {FormatDemo fd = new FormatDemo (); fd. format1 ("###,###. ### ", 111222.34567); // the front side is not enough to add 0, and the backend is not enough to make up 0 fd. format1 ("000,000.000", 11222.34567); fd. format1 ("###,###. ### ¥ ", 111222.34567); fd. format1 ("000,000.000 $", 11222.34567); // The number of fd. format1 ("##. ### % ", 0.345678); // The number of fd. format1 ("00. ### % ", 0.0345678); // Number of thousands of fd. format1 ("###. ###/u2030 ", 0.345678); // exponential form DecimalFormat df1 = new DecimalFormat (); // use applypattern df1.applyPattern (" 0.000E0000 "); System. out. println (df1.format (111222.34567 ));}}
Program running result:
###,###. ### Style formatting: 111222.34567 get: 111,222.346 use 000,000.000 style formatting: 11222.34567 get: 011,222.346 use ###,###. ### $ format: 111222.34567 get: 111,222.346 use 000,000.000 format: 11222.34567 get: 011,222.346 use ##. ### % format: 0.345678 get: 34.568% use 00. ###% style formatting: 0.0345678 get: 03.457% use ###. ### ‰ style formatting: 0.345678 get: 345.678 ‰ 1.20.e0005
DecimalFormat format template
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.