java reserved Two-bit decimals problem: Way One: RoundingDoublef = 111231.5585; BigDecimal b=NewBigDecimal (f); DoubleF1 = B.setscale (2, bigdecimal.round_half_up). Doublevalue (); Keep two decimal places---------------------------------------------------------------mode two: Java.text.DecimalFormat DF=NewJava.text.DecimalFormat ("#.00"); Df.format (the number you want to format);NewJava.text.DecimalFormat ("#.00"). Format (3.1415926)#.00represents 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 point 2The 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, the last bit shown is roundedImportjava.text.* ; Importjava.math.* ; classTT { Public Static voidMain (String args[]) {Doublex=23.5455; NumberFormat ddf1=numberformat.getnumberinstance (); Ddf1.setmaximumfractiondigits (2); String s=Ddf1.format (x); System.out.print (s); } }
Reprint Address: http://blog.csdn.net/smalloceanlxh/article/details/5778659
Android Division preserves decimal points