Math. Round (11.5) returns (long) 12, math. Round (-11.5) returns (long)-11;
The math. Round () method adds the parameter 0.5 first and then obtains its math. Floor () value,Math. Floor ()You can obtain the integer part of a number,Math. FloorReturns the largest integer but not greater than itself.,Instead of rounding
According to the operation of round () in JavaAlgorithmThen (long) math. Floor (a + 0.5d ),
When-8.5 <A <-8.0 such as a =-8.4
The return value is
(Long) math. Floor (-8.4 + 0.5d)
= (Long) math. Floor (-1, 7.9)
=-8
This operation is correct,
When a =-8.5 returns (long) math. Floor (-8.0) =-8
When-9 <A <-8.5 for example, a =-8.6 returns (long) math. Floor (-8.1) =-9
These results are correct from the operational logic in Java. Different from the calculation results in Excel, they have different definitions for the round () method.
So round (-8.5) = floor (-8.0) =-8, while round (-8.6) = floor (-8.1) =-9;
System. Out. println (-8.1 + "" + math. Round (-8.1 ));
System. Out. println (-8.2 + "" + math. Round (-8.2 ));
System. Out. println (-8.3 + "" + math. Round (-8.3 ));
System. Out. println (-8.4 + "" + math. Round (-8.4 ));
System. Out. println (-8.5 + "" + math. Round (-8.5 ));
System. Out. println (-8.6 + "" + math. Round (-8.6 ));
System. Out. println (-8.7 + "" + math. Round (-8.7 ));
System. Out. println (-8.8 + "" + math. Round (-8.8 ));
System. Out. println (-8.9 + "" + math. Round (-8.9 ));
}
Running result:
-8.1-8
-8.2-8
-8.3-8
-8.4-8
-8.5-8
-8.6-9
-8.7-9
-8.8-9
-8.9-9