Alex Rivera | Logout

How do I round a number in JavaScript?

Asked 2008-10-29T09:14:21.250
68

The values can sometimes be floats (with different precision), integers, or even in the form 2.20011E+17. I want to output pure integers. How do I write a method that takes these sometimes-floats and makes them integers?

Edit
Report

1 Answer

16

You can use Math.round() for rounding numbers to the nearest integer.

Math.round(532.24) => 532

Also, you can use parseInt() and parseFloat() to cast a variable to a certain type, in this case integer and floating point.

answered 2008-10-29T09:19:50.897

Your Answer