Rounding of Coarse force is an integer
1 PackageMath; 2 3 Public classMathroundtest {4 /** 5 * Three methods related to rounding are provided in the Math class: Ceil,floor,round,6 * These methods correspond to the meanings of their English names, for example: the English meaning of Ceil is the ceiling, which means rounding up,7 * Math.ceil (11.3) Results for 12,math.ceil (-11.6) results for -11;floor English is the floor,8 * The method represents a downward rounding, and the result of Math.floor (11.6) is the result of 11,math.floor (-11.4) -12;9 * The most difficult to master is the round method, he said, "rounding", the algorithm for Math.floor (x+0.5), the original number plus 0.5 and then rounded down,Ten * Therefore, the result of Math.Round (11.5) is that the result of 12,math.round (-11.5) is-11. Math.Round () conforms to this rule: One * After the decimal point is greater than 5 all plus, equal to 5 positive plus, less than 5 all without adding. A */ - Public Static voidMain (string[] args) { -System.out.println ("The first =5 after the decimal place"); theSystem.out.println ("Positive: 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 <5 after the decimal place"); -System.out.println ("Positive: Math.Round (11.46) =" + Math.Round (11.46)); +SYSTEM.OUT.PRINTLN ("Negative number: Math.Round ( -11.46) =" + Math.Round (-11.46)); A System.out.println (); at -System.out.println ("The first >5 after the decimal place"); -System.out.println ("Positive: Math.Round (11.68) =" + Math.Round (11.68)); -SYSTEM.OUT.PRINTLN ("Negative number: Math.Round ( -11.68) =" + Math.Round (-11.68)); - /** - * Operation Result: in 1, the first place after the decimal =5 - 2. Positive number: Math.Round (11.5) =12 to 3, Negative number: Math.Round ( -11.5) =-11 + 4. - 5, the first place after the decimal <5 the 6. Positive number: Math.Round (11.46) =11 * 7, Negative number: Math.Round ( -11.46) =-11 $ 8.Panax Notoginseng 9, the first place after the decimal >5 - 10. Positive number: Math.Round (11.68) =12 the 11, Negative number: Math.Round ( -11.68) =-12 + */ A /** the * 1, the first digit after the decimal point <5, the result of the operation is the integer part of the parameter. + 2, the parameter of the first digit after the >5, the result of the parameter integer part of the absolute value +1, the symbol (that is, plus or minus) unchanged. - 3, the parameters of the first digit after the decimal = 5, the positive result is the integer part +1, the result of the negative operation is an integer part. $ */ $ } - -}
View Code
Rounding algorithm for Java