Java uses DecimalFormat for numeric formatting examples

Source: Internet
Author: User

//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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.