1 PackageMath;2 3 Public classTestmath_round {4 Public Static voidMain (string[] args) {5SYSTEM.OUT.PRINTLN (Math.Round (0.5));//16System.out.println (Math.Round (-0.5));//07System.out.println (Math.Round (-0.501));//-18 //the Math class rounding method round for negative operations when the decimal place is greater than 0.5 to carry, less than or equal to 0.5 not rounding9 }Ten}
View Code
1 PackageMath;2 3 ImportJava.math.BigDecimal;4 5 classmyround{6 Public Static DoubleDivDoubleDintScale ) {7BigDecimal b1=NewBigDecimal (d);8BigDecimal b2=NewBigDecimal (1);9 returnb1.divide (B2, scale, bigdecimal.round_half_up). Doublevalue ();Ten } One } A Public classTestmath_round { - Public Static voidMain (string[] args) { -SYSTEM.OUT.PRINTLN (Math.Round (0.5));//1 theSystem.out.println (Math.Round (-0.5));//0 -System.out.println (Math.Round (-0.501));//-1 - //the Math class rounding method round for negative operations when the decimal place is greater than 0.5 to carry, less than or equal to 0.5 not rounding -System.out.println (Myround.div (-0.5, 0));//-1.0 + //after the improvement for the usual rounding. - } +}
improved usually rounded
"Java" Java.lang.Math:public Static long round (double a) and public static int round (float a)