Round to Nearest
Rounds a number or numeric string to the closest multiple of a specified interval.
roundToNearest
The roundToNearest function rounds a number (or numeric string) to the closest multiple of a specified interval. This is particularly useful for creating stepped values, quantization, or rounding to common increments like 5, 10, or 0.5.
Function Signature
function roundToNearest(value: Numeric, interval?: number): numberParameters
value(Numeric): The number or numeric string to round.interval(number, optional): The rounding increment (default:5).
Return Value
number: The input value rounded to the nearest multiple of the specified interval.
Example Usage
playground.ts
Notes
- Zero Handling: If
intervalis0, the function returnsNaN. - Standard mathematical rounding rules apply (values
>= 0.5of the interval round up, values< 0.5round down).
Aliases
The following aliases can be used for the roundToNearest function:
roundNumberToNearestIntervalroundToNearestInterval
See Also
- roundNumber - For simple decimal place rounding.
- convertToDecimal - For fixed decimal place formatting.
Last updated: Sun, Jun 14, 2026 07:52:00PM (UTC)
