//method of obtaining DecimalFormat decimalformat.getinstance (); Public Static voidtest1 (DecimalFormat df) {//3 decimal places are displayed by default DoubleD = 1.5555555; System.out.println (Df.format (d));//1.556//set the maximum number of digits after the decimal point is 5Df.setmaximumfractiondigits (5); Df.setminimumintegerdigits (15); System.out.println (Df.format (d));//1.55556Df.setmaximumfractiondigits (2); System.out.println (Df.format (d));//1.56//set the minimum number of digits after the decimal point, not enough time to fill 0Df.setminimumfractiondigits (10); System.out.println (Df.format (d));//1.5555555500//set the integer part to a minimum length of 3, and fill 0 when not enoughDf.setminimumintegerdigits (3); System.out.println (Df.format (d)); //set the integer part to the maximum value of 2, and when it is over, the corresponding number of digits is taken from the single digit.Df.setmaximumintegerdigits (2); System.out.println (Df.format (d)); } Public Static voidtest2 (DecimalFormat df) {intNumber = 155566; //The default integer part is divided into three groups,SYSTEM.OUT.PRINTLN (number);//output Format 155,566//set each of the four groupsDf.setgroupingsize (4); System.out.println (Df.format (number));//output format is 15,5566DecimalFormatSymbols DFS =decimalformatsymbols.getinstance (); //Set Decimal SeparatorDfs.setdecimalseparator ('; ')); //Set grouping separatorsDfs.setgroupingseparator (' a '); Df.setdecimalformatsymbols (DFS); System.out.println (Df.format (number));//15a5566System.out.println (Df.format (11.22));//11;22//UngroupDf.setgroupingused (false); System.out.println (Df.format (number)); } Public Static voidtest3 (DecimalFormat df) {DoubleA = 1.220; Doubleb = 11.22; Doublec = 0.22; //placeholders can use 0 and # Two, when using 0 will be strictly according to the style to match, when not enough time will be 0, and use # will be used to ignore the front and back 0//output as a percentage//Df.applypattern ("00.00%");Df.applypattern ("##.##%"); System.out.println (Df.format (a));//122%System.out.println (Df.format (b));//1122%System.out.println (Df.format (c));//22% DoubleD = 1.22222222; //output as a fixed formatDf.applypattern ("00.000"); System.out.println (Df.format (d));//01.222Df.applypattern ("##.###"); System.out.println (Df.format (d));//1.222}
Java uses DecimalFormat for numeric formatting examples