Home > Web Front-end > JS Tutorial > body text

Comparison of the three major rounding functions of js (round(), ceil(), floor())

php是最好的语言
Release: 2018-08-10 17:44:54
Original
3216 people have browsed it

Decimals often need to be rounded. js provides us with three rounding functions: round(), ceil(), floor(). Each of them has its own purpose. In daily life and scientific research data, rounding is often necessary. This is also the most common rounding method we have encountered since childhood. The round() function of js can satisfy our needs. Requirements, the word round originally means an integer, which is also easy to understand. Ceil means ceiling. Foreigners especially like visual expressions. We can guess that the ceiling must increase the decimal by 1 when rounding, and the floor must be exactly the opposite. I remember that there is such an expression in high school mathematics. Use the symbol [] to call it a Gaussian function.

  • round(): Rounding

  • ceil(): Rounding up

  • floor(): Round down (Gaussian function)

However, what is the use of each of them?

#round()

It is most used in daily life. For example, when we go shopping in a mall, many products actually have decimals, but when paying, Often stores will round, such as this:

Copy after login

Result:

毛衣的价格:99元
辣条的价格:1.49元
洗发露的价格:29元
T恤的价格:69元
毛笔的价格:17元
26 应付款:215元
Copy after login
Copy after login

ceil()

But sometimes the store is very stingy and won’t I'll give you those few cents, but let you pay a few more cents:

Copy after login

Result:

毛衣的价格:99元
辣条的价格:1.49元
洗发露的价格:29元
T恤的价格:69元
毛笔的价格:17元
26 应付款:216元
Copy after login

floor()

Of course , sometimes the merchant will be very good at doing business and give you a few cents. He is neither as mechanical as the first one nor as stingy as the second one.

Copy after login

Result:

毛衣的价格:99元
辣条的价格:1.49元
洗发露的价格:29元
T恤的价格:69元
毛笔的价格:17元
26 应付款:215元
Copy after login
Copy after login

In practical applications, the three rounding functions are not limited to arithmetic problems in life. They are actually used in a variety of ways. For example, we Randomly draw integers, although we can use random to get random decimals, but sometimes we also need integers, which requires rounding according to actual needs.

Related recommendations:

Advanced JavaScript (5) Some functions of taking decimals and integers in js

Introduction to JavaScript Advanced Programming

The above is the detailed content of Comparison of the three major rounding functions of js (round(), ceil(), floor()). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!