Home >Web Front-end >JS Tutorial >How to round data in JavaScript
There are several methods to implement data rounding: round method, tofixed method, parseInt method, ceil method and floor method
Rounding of values in JavaScript There are the following
round() methods: a number can be rounded to the nearest integer, that is, rounding
toFixed() method: a Number can be rounded to the specified number of decimal places number.
parseInt() method: can convert decimals into integers (bit operators)
ceil() method: can round up a number
floor() Method: A number can be rounded down
The specific usage of these methods will be introduced in detail in the article
round() method
document.write(Math.round(4.55) + "<br>"); document.write(Math.round(-6.45));
Rendering:
##toFixed() method
var num=3.141592; document.write(num.toFixed(2));Rendering: ##parseInt() method
document.write(parseInt("12.333"));
Rendering:
document.write("ceil方法:")
document.write(Math.ceil("12.333") + "<br>");
document.write("floor方法:")
document.write(Math.floor("12.333"));
Rendering:
The above is the detailed content of How to round data in JavaScript. For more information, please follow other related articles on the PHP Chinese website!