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): numberParameters
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.
- Positive values: Number of decimal places to round to (default:
Return Value
number: The rounded number.
Example Usage
playground.ts
Comparison with Similar Functions
| Feature | roundNumber | convertToDecimal | roundToNearest |
|---|---|---|---|
| Decimal places | ✓ | ✓ | ✗ |
| Negative places | ✓ | ✗ | ✗ |
| String output option | ✗ | ✓ | ✗ |
| Arbitrary intervals | ✗ | ✗ | ✓ |
Aliases
The following aliases can be used for the roundNumber function:
roundToDecimal
See Also
- convertToDecimal - For fixed decimal place formatting.
- roundToNearest - For rounding to arbitrary intervals (like nearest 5, 0.5, etc.).
Last updated: Sun, Jun 14, 2026 07:06:16AM (UTC)
