Which method of rounding is provided in Java by math

Source: Internet
Author: User
The three methods that are available in the math class relate to rounding: first one: Ceil

Ceil means: The meaning of the ceiling; The method indicates that the value of the Math.ceil (11.3) upward is 12; Math.ceil. (-11.6) The result is-11; the second one is: Floor

First of all his English meaning is the meaning of the floor, which means that the downward rounding,
The result of Math.floor (11.6) is 11;
Math.floor (-11.4); The result is -12. The third is: round

He said rounding, the algorithm is Math.floor (x+0.5), that is, add 0.5 to the original number and then proceed down the rounding;
Math.Round (11.5), i.e. Math.floor (11.5+0.5) = Math.floor (12) = 12;
Similarly: Math.Round ( -11.5) = Math.floor ( -11.5+0.5) =math.floor (-11.0) =-11; Conclusion:

Conclusion: After the decimal point is greater than 5, the decimal point is less than and equal to 5, and is more than 5 of the rounding
That is to say: After the decimal point is greater than 5 all add, equal to 5 positive plus, less than 5 all do not add
Self-feel, or use Math.floor (x+0.5) way to memory, plus 0.5 after the whole line down!

Related Article

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.