Rounding in JAVA using the Math.Round (T-a) function, the function returns a long type, which can be double or float.
View JDK Source code:
public static long round (double a) { if (a = 0x1.fffffffffffffp-2)//greatest Double value less than 0.5 return (long) floor (a + 0.5d); else return 0;}
public static double floor (double a) { return Strictmath.floor (a),//default impl. Delegates to Strictmath}
public static double floor (double a) { return Floororceil (A,-1.0, 0.0,-1.0);}
In fact, concrete implementation is quite complicated.
It is important to note that a is a negative case.
1System.out.println ("The first =5 after the decimal place");2System.out.println ("Positive: math.round (11.5) =" + Math.Round (11.5));3SYSTEM.OUT.PRINTLN ("Negative number: Math.Round ( -11.5) =" + Math.Round (-11.5));4 System.out.println ();5System.out.println ("The first <5 after the decimal place");6System.out.println ("Positive: Math.Round (11.46) =" + Math.Round (11.46));7SYSTEM.OUT.PRINTLN ("Negative number: Math.Round ( -11.46) =" + Math.Round (-11.46));8 System.out.println ();9System.out.println ("The first >5 after the decimal place");TenSystem.out.println ("Positive: Math.Round (11.68) =" + Math.Round (11.68)); OneSystem.out.println ("Negative: Math.Round ( -11.68) =" + Math.Round (-11.68));
The output is:
First digit after decimal point =5 positive: Math.Round (11.5) =12 negative: Math.Round (-11.5) =-11 first digit after decimal point <5 Positive number: Math.Round (11.46) =11 negative: Math.Round (-11.46) =-11 the first digit after the decimal point >5 positive: Math.Round ( 11.68) =12 negative: Math.Round (-11.68) =-12
Positive number and we usually learn the same, negative numbers in the decimal point after the first 5 o'clock, directly to the fractional portion, greater than 5 o'clock minus one, less than 5 o'clock directly to the fractional part.
Math.Round ()