[Home] > Snippets  > Languages  > JavaScript  > Math  >  Round a number to the nearest multiple of a given value

Round a number to the nearest multiple of a given value

JavaScript

const roundNearest = (value, nearest) => Math.round(value / nearest) * nearest

TypeScript

const roundNearest = (value: number, nearest: number): number => Math.round(value / nearest) * nearest

Examples

roundNearest(100, 30) // 90
roundNearest(200, 30) // 210
roundNearest(200, 40) // 200