Math. Round (11.5) and so on? Math. Round (-11.5) and so on?

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.