Several methods for rounding small values to integers

Source: Internet
Author: User
Tags rounds

Here are a few ways to round decimal values to integers: Math.ceil (), Math.floor (), and Math.Round (). These three methods follow the following rounding rules, respectively:
Math.ceil () performs an upward rounding, that is, it always rounds the value up to the nearest integer;
Math.floor () Performs a downward rounding, that is, it always rounds the value down to the nearest integer;
Math.Round () performs a standard rounding, that is, it always rounds a number to the nearest integer (which is also the rounding rule we learned in math Class).

The following are examples of using these methods:

?
123456789 alert (Math.ceil (25.9)); //26 alert (Math.ceil (25.5)); //26 alert (Math.ceil (25.1)); //26 alert (Math.Round (25.9)); //26 alert (Math.Round (25.5)); //26 alert (Math.Round (25.1)); //25 alert (Math.floor (25.9)); //25 alert (Math.floor (25.5)); //25 alert (Math.floor (25.1)); //25

Nanchang Network Company Technical Staff Summary: for all values between 25 and 26 (excluding 26), Math.ceil () always returns 26 because it performs rounding up. The Math.Round () method returns 26 only if the value is greater than or equal to 25.5, or 25. Finally, Math.floor () returns 25 for all values between 25 and 26 (not including 26).

Here are some additions:
Ceil (): The fractional part is rounded to the integer part.
Such as:

Math.ceil (12.2)//return 13
Math.ceil (12.7)//return 13
Math.ceil (12.0)//return 12

Floor (): All are left, only integers are reserved.
Such as:

Math.floor (12.2)//return 12
Math.floor (12.7)//return 12
Math.floor (12.0)//return 12

Round (): Rounding
Such as:

Math.Round (12.2)//return 12
Math.Round (12.7)//return 13
Math.Round (12.0)//return 12

Several methods for rounding small values to integers

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.