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?
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:
<script type="text/javascript"> function gro(){ var a=new Array(); a[0]=99;a[1]=1.49;a[2]=29;a[3]=69;a[4]=17; console.log("毛衣的价格:"+a[0]+"元"); console.log("辣条的价格:"+a[1]+"元"); console.log("洗发露的价格:"+a[2]+"元"); console.log("T恤的价格:"+a[3]+"元"); console.log("毛笔的价格:"+a[4]+"元"); var sum=0; for(var i=0;i<5;i++){ sum+=a[i]; } //收钱 console.log("应付款:"+Math.round(sum)+"元"); } gro(); </script>
Result:
毛衣的价格:99元 辣条的价格:1.49元 洗发露的价格:29元 T恤的价格:69元 毛笔的价格:17元 26 应付款:215元
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:
<script type="text/javascript"> function cei(){ var a=new Array(); a[0]=99;a[1]=1.49;a[2]=29;a[3]=69;a[4]=17; console.log("毛衣的价格:"+a[0]+"元"); console.log("辣条的价格:"+a[1]+"元"); console.log("洗发露的价格:"+a[2]+"元"); console.log("T恤的价格:"+a[3]+"元"); console.log("毛笔的价格:"+a[4]+"元"); var sum=0; for(var i=0;i<5;i++){ sum+=a[i]; } //收钱 console.log("应付款:"+Math.ceil(sum)+"元"); } cei(); </script>
Result:
毛衣的价格:99元 辣条的价格:1.49元 洗发露的价格:29元 T恤的价格:69元 毛笔的价格:17元 26 应付款:216元
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.
<script type="text/javascript"> function flo(){ var a=new Array(); a[0]=99;a[1]=1.49;a[2]=29;a[3]=69;a[4]=17; console.log("毛衣的价格:"+a[0]+"元"); console.log("辣条的价格:"+a[1]+"元"); console.log("洗发露的价格:"+a[2]+"元"); console.log("T恤的价格:"+a[3]+"元"); console.log("毛笔的价格:"+a[4]+"元"); var sum=0; for(var i=0;i<5;i++){ sum+=a[i]; } //收钱 console.log("应付款:"+Math.floor(sum)+"元"); } flo(); </script>
Result:
毛衣的价格:99元 辣条的价格:1.49元 洗发露的价格:29元 T恤的价格:69元 毛笔的价格:17元 26 应付款:215元
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!