These three methods are a headache. After a burst of searches, We will summarize them as follows: Math. Round: If the parameter is a decimal number, rounding itself. Math. ceil: If the parameter is a decimal number, the smallest integer is obtained but not smaller than itself. math. floor: If the parameter is a decimal number, calculate the largest integer but not greater than itself. at the same time, pay attention to their return type: long round (double A) int round (float a) Double Ceil (double A) Double floor (double) Code As follows:
-
- Public ClassRoundtest {
-
-
- Public Static VoidMain (string [] ARGs ){
-
- Double[] Values = {-2.3,-1.0,0.25,4,1.5};
-
-
- For(IntI =0; I <values. length; I ++ ){
-
- System. Out. println (math. Round (Values [I]);
-
-
- System. Out. println (math. Ceil (Values [I]);
-
-
- System. Out. println (math. Floor (Values [I]);
-
- System. Out. println ("============"+ I +"============");
-
-
- }
-
-
- }
-
-
- }
The returned results are as follows: -2-2.0-3.0 ============= 0 =================-1-1.0-1.0 ==== = ===== 44.04.0 ================= 3 ================= 22.01.0 ========= = 4 ================