| Viewed 130 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
Answered
20

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