[Common Java class libraries] _ numberformat
Import Java. text. *; public class numberformatdemo01 {public static void main (string ARGs []) {numberformat NF = NULL; // declare a numberformat object NF = numberformat. getinstance (); // get the default number format to display system. out. println ("formatted number:" + NF. format (10000000); system. out. println ("formatted number:" + NF. format (1000.345 ));}};
Output:
Formatted number: 10,000,000
Formatted number: 1,000.345
Instantiation template:
Import Java. text. *; Class formatdemo {public void format1 (string pattern, double value) {// This method is specially used for formatting and displaying decimalformat df = NULL; // declare the decimalformat Class Object df = new decimalformat (pattern); // instantiate the object and input the template string STR = DF. format (value); // format the number system. out. println ("use" + pattern + "format number" + value + ":" + Str) ;}}; public class numberformatdemo02 {public static void main (string ARGs []) {formatdemo demo = new formatdemo (); // demo of the formatting object class. format1 ("###,###. ### ", 111222.34567); demo. format1 ("000,000.000", 11222.34567); demo. format1 ("###,###. ### ¥ ", 111222.34567); demo. format1 ("000,000.000 $", 11222.34567); demo. format1 ("##. ### % ", 0.345678); demo. format1 ("00. ### % ", 0.0345678); demo. format1 ("###. ### \ u2030 ", 0.345678 );}};
Output:
####,###### Format the number 111222.34567: 111,222.346
Use 000,000.000 to format the number 11222.34567: 011,222.346
Use ####,#######¥ format the number 111222.34567: 111,222.346 ¥
Use 000,000.000 ¥ to format the number 11222.34567: 011,222.346 ¥
Use ##. ###% to format the number 0.345678: 34.568%
Use 00. ###% to format the number 0.0345678: 03.457%
####### Format the number 0.345678: 345.678 ‰