Java Math.Round () is a special rounding of the public class Mathtest {
public static void Main (string[] args) {
System.out.println ("The first digit after the decimal point =5");
SYSTEM.OUT.PRINTLN ("Positive number: Math.Round (11.5) =" + Math.Round (11.5));
SYSTEM.OUT.PRINTLN ("Negative number: Math.Round ( -11.5) =" + math.round (-11.5));
System.out.println ();
System.out.println ("The first digit after the decimal point <5");
SYSTEM.OUT.PRINTLN ("Positive number: Math.Round (11.46) =" + Math.Round (11.46));
SYSTEM.OUT.PRINTLN ("Negative number: Math.Round ( -11.46) =" + Math.Round (-11.46));
System.out.println ();
System.out.println ("The first digit after the decimal point >5");
SYSTEM.OUT.PRINTLN ("Positive number: Math.Round (11.68) =" + Math.Round (11.68));
SYSTEM.OUT.PRINTLN ("Negative number: Math.Round ( -11.68) =" + Math.Round (-11.68));
}
}
Run Result:
1, the first place after the decimal point =5
2. Positive number: Math.Round (11.5) =12
3, Negative: Math.Round (-11.5) =-11
4,
5, the first place after the decimal point <5
6. Positive number: Math.Round (11.46) =11
7, Negative: Math.Round (-11.46) =-11
8,
9, the first place after the decimal point >5
10. Positive number: Math.Round (11.68) =12
11, Negative: Math.Round (-11.68) =-12
Based on the results of the above example, we can also summarize in the following way, perhaps more easily memory:
1, the first digit after the decimal point of the parameter, the result of the <5 is the integer part of the parameter.
2, the first digit after the decimal point >5, the result is the parameter integer part absolute value +1, the symbol (i.e. positive or negative) unchanged.
3. After the decimal point of the parameter is the first digit = 5, the positive result is an integer part +1, and the negative operation result is an integral part.
End: Greater than five all plus, equal to five positive plus, less than five total.
Another problem is the principle of odd couples.
Today, the client came to me and said, our program inside the calculation of the price is not correct, I checked, found that prices are converted after the price, the result is likely to be more decimal places, and documents can only print two-bit price, so the price call Math.Round (price,2) function rounding.
The problem is the unit price is 1.805, function Math.Round (1.805,2) return value is 1.80, according to the principle of rounding, the result should be 1.81.
After Google, we found out that Microsoft was right and that we were wrong. :(
The original rounding also has an international practice, called Chi Jin, meaning that when rounding the front of an odd number, on the go, for even when, on the house, which embodies the principle of fairness.
However, international practice is often not feasible in domestic enterprises, in order to meet their requirements, the use of Math.Round (Price,2,midpointrounding.awayfromzero) on it.