Differences between JavascriptMathceil (), floor (), and round () Functions

Source: Internet
Author: User
Round is rounded in... Ceiling is rounded up .. Float is a number of methods to round down a small value to an integer: Math. ceil (), Math. floor (), and Math. round (). The three methods follow the following rounding rules:
◎ Math. ceil () rounds up, that is, it always rounds up the value to the nearest integer;
◎ Math. floor () performs downward rounding, that is, it always rounds down the value to the nearest integer;
◎ Math. round () performs standard rounding, that is, it always rounds the value to the nearest integer (this is also the Rounding Rule we learned in mathematics ).

The following is an example of using these methods:

alert(Math.ceil(25.9)); //26alert(Math.ceil(25.5)); //26alert(Math.ceil(25.1)); //26alert(Math.round(25.9)); //26alert(Math.round(25.5)); //26alert(Math.round(25.1)); //25alert(Math.floor(25.9)); //25alert(Math.floor(25.5)); //25alert(Math.floor(25.1)); //25

Summary: For all values between 25 and 26 (excluding 26), Math. ceil () always returns 26 because it is rounded up. The Math. round () method returns 26 only when the value is greater than or equal to 25.5. Otherwise, 25 is returned. Finally, Math. floor () returns 25 for all values between 25 and 26 (excluding 26.

Below are some supplements:
Ceil (): returns the decimal part to the integral part.
For example:

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

Floor (): all are rounded off and only integers are retained.
For example:

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

Round (): Rounding
For example:

Math. round (12.2) // returns 12
Math. round (12.7) // return 13
Math. round (12.0) // returns 12

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.