Javascript division and rounding methods include: 1. Rounding is achieved through the round method; 2. Rounding up is achieved through the ceil method; 3. Rounding down is achieved through the floor method; 4. Forced through parseInt Rounding.
The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.
What are the methods of javascript division and rounding?
Four methods of javascript division results rounding
Math.round(7/2)=4 //四舍五入,round() 方法可把一个数字舍入为最接近的整数。 Math.ceil(7/2)=4 //强制向上取整,ceil() 方法将数字向上舍入为最接近的整数,并返回结果。如果传递的参数是整数,则不会舍入该值。 Math.floor(7/2)=3 //强制向下取整,floor() 方法返回小于等于x的最大整数。如果传递的参数是一个整数,该值不变 parseInt(7/2)=3 //强行转化,得出最小整数,同Math.floor方法。parseInt() 函数可解析一个字符串,并返回一个整数。
Recommended study: " JavaScript basic tutorial》
The above is the detailed content of What are the methods of javascript division and rounding?. For more information, please follow other related articles on the PHP Chinese website!