Skip to content

Number

Functions to manipulate numbers immutably and type-safely. All functions preserve the original values and return a new value.

How to import?

The library exposes the DNumber and N namespaces from the main entry or via direct import (tree-shaking friendly), which lets you only load what you need.

typescript
import { DNumber, N } from "@duplojs/utils";
import * as DNumber from "@duplojs/utils/number";
import * as N from "@duplojs/utils/number";

Arithmetic operations

add

Adds two numbers.

subtract

Subtracts one number from another.

multiply

Multiplies two numbers.

divide

Divides one number by another.

modulo

Returns the remainder of the division between two numbers.

power

Raises a number to a specified power.

negate

Inverts the sign of a number.

Math functions

abs

Returns the absolute value of a number.

round

Rounds a number to the nearest integer.

ceil

Rounds a number up.

floor

Rounds a number down.

trunc

Returns the integer part of a number.

max

Returns the largest number in a tuple.

min

Returns the smallest number in a tuple.

clamp

Clamps a number between a minimum and maximum value.

sqrt

Returns the square root of a number.

toFixed

Formats a number with a fixed number of decimals.

Trigonometric functions

sin

Returns the sine of a number (in radians).

cos

Returns the cosine of a number (in radians).

tan

Returns the tangent of a number (in radians).

asin

Returns the arcsine of a number.

acos

Returns the arccosine of a number.

atan

Returns the arctangent of a number.

atan2

Returns the arctangent of the quotient of two numbers.

Comparison

greater

Checks whether a number is strictly greater than another.

greaterThan

Checks whether a number is greater than or equal to another.

less

Checks whether a number is strictly less than another.

lessThan

Checks whether a number is less than or equal to another.

Released under the MIT license.