Toolbox-XToolbox-X

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): number

Parameters

  • 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 interval is 0, the function returns NaN.
  • Standard mathematical rounding rules apply (values >= 0.5 of the interval round up, values < 0.5 round down).

Aliases

The following aliases can be used for the roundToNearest function:

  • roundNumberToNearestInterval
  • roundToNearestInterval

See Also

Last updated: Sun, Jun 14, 2026 07:52:00PM (UTC)

On this page