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

Round a number to the nearest multiple of a given value

JavaScript version

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

TypeScript version

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