Possible Duplicate:
How can I round of to whole numbers in JavaScript?
In javascript is there a function which allows me to round to the nearest integer? Either up or down.
So:
2.1 = 2
2.9 = 3
4.5 = 5
1.1 = 1
round() The Math. round() function returns the value of a number rounded to the nearest integer.
Using the Custom Function to Round a Number To2 Decimal Places in JavaScript. function roundToTwo(num) { return +(Math. round(num + "e+2") + "e-2"); } console. log(roundToTwo(2.005));
The Math. round() function in JavaScript is used to round the number passed as parameter to its nearest integer. Parameters : The number to be rounded to its nearest integer.
Use Math.round(number)
:
var i = Math.round(2.1);
Math.round(x)
and Math.floor(x)
both documented here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With