| Viewed 154 times

How to Round to the Nearest in javaScript?

I need a function that takes two numbers as arguments and returns the first number rounded to the nearest number, or a multiple of that number.

1 Answer
Mohammad Qandeel

there are many solutions to Round a number 

use this function 

function roundNearest(num, nearest) {
 if (!nearest) return Math.round(num)
 return ~~((num+Math.sign(num)*nearest/2)/nearest)*nearest

Related Questions