Number UtilitiesAPI reference for number manipulation functionshashAPI Reference
Categories

Number Utilities

The Number utilities provide functions for manipulating and formatting numbers in JavaScript.

Functions

roundNumber

function roundNumber(number, digits = 5)

Rounds a number to a specified number of decimal places.

Precision Handling This function uses a scaling factor to handle floating-point precision issues that can occur with simple Math.round() calls.

Parameters

Name Type Default Description
number number The number to round
digits number 5 The number of decimal places to round to

Returns

The rounded number.

Example

import { roundNumber } from '@semantic-ui/utils';
console.log(roundNumber(3.14159, 2)); // 3.14
console.log(roundNumber(1.005, 2)); // 1.01
console.log(roundNumber(1234.5678)); // 1234.56780 (default 5 digits)

roundDecimal

function roundDecimal(number, digits = 5)

Rounds a number to a specified number of decimal places.

Precision Handling This function uses a scaling factor to handle floating-point precision issues that can occur with simple Math.round() calls.

Parameters

Name Type Default Description
number number The number to round
digits number 5 The number of decimal places to round to

Returns

The rounded number.

Example

import { roundDecimal } from '@semantic-ui/utils';
roundDecimal(123.456789) // returns 123.45
roundDecimal(0.00123456789, 3) // returns 0.001
roundDecimal(123, 3) // returns 123.000
roundDecimal(Infinity) // returns Infinity
Previous
Looping
Next
Objects