Java multiple methods for keeping a double two decimal places

Source: Internet
Author: User

Java keeps a double of two decimal places in a multi-method Java reserved Two-bit decimal mport Java.text.DecimalFormat; DecimalFormat DF=NewDecimalFormat ("##### #0. XX"); DoubleD1 =3.23456  DoubleD2 =0.0;DoubleD3 =2.0;d F.format (D1); Df.format (D2); Df.format (D3); 3 results were:3.230.00 2.00java reserved Two-bit decimals problem: Way One: RoundingDoublef =111231.5585; BigDecimal b=NewBigDecimal (f); DoubleF1 = B.setscale (2, bigdecimal.round_half_up). Doublevalue (); Reserved two-bit decimal way two: Java.text.DecimalFormat DF=NewJava.text.DecimalFormat ("#.00"); Df.format (the number you want to format);NewJava.text.DecimalFormat ("#.00"). Format (3.1415926)#.xxrepresents two decimal places #.0000 four decimal places and so on ... Way three:DoubleD =3.1415926; String result= String. Format ("%.2f");%.2f%. Represents any number of digits before the decimal point2The result of representing a two-bit decimal format is F for floating-point mode four: NumberFormat ddf1=numberformat.getnumberinstance ();voidSetmaximumfractiondigits (intdigits) digits displays the number of digits for the formatted object to set the maximum number of digits to display after the decimal point, and the last bit shown is the rounded import java.text.*; import Java.math.* ; classTT { Public Static voidMain (String args[]) {Doublex=23.5455; NumberFormat ddf1=numberformat.getnumberinstance ();dd f1.setmaximumfractiondigits (2); String s=Ddf1.format (x); System. out. Print (s);} }import java.text.*;D Ecimalformat DF=NewDecimalFormat (".##");DoubleD=1252.2563; String St=Df.format (d); System. out. println (ST);

Java multiple methods for keeping a double two decimal places

Related Article

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.