Toolbox-XToolbox-X

Round A Number

Rounds a number or numeric string to a specified number of decimal or integer places.

roundNumber

The roundNumber function provides flexible rounding capabilities, allowing rounding to both positive (right of decimal) and negative (left of decimal) decimal places. This makes it useful for both fractional rounding and whole number rounding scenarios.

Function Signature

function roundNumber(number: Numeric, roundTo?: number): number

Parameters

  • number (Numeric): The number or numeric string to round.
  • roundTo (number, optional):
    • Positive values: Number of decimal places to round to (default: 2).
    • Negative values: Rounds to the left of the decimal (e.g., -1 = tens, -2 = hundreds).
    • 0: Rounds to the nearest integer.

Return Value

  • number: The rounded number.

Example Usage

playground.ts

Comparison with Similar Functions

FeatureroundNumberconvertToDecimalroundToNearest
Decimal places
Negative places
String output option
Arbitrary intervals

Aliases

The following aliases can be used for the roundNumber function:

  • roundToDecimal

See Also

Last updated: Sun, Jun 14, 2026 07:06:16AM (UTC)

On this page