Rounds to nearest number, with ties rounded away from zero.
+Rounds to nearest number, with ties rounded towards the nearest even number.
+Rounds to nearest number, with ties rounded towards -Infinity.
+Rounds to nearest number, with ties rounded towards +Infinity. Similar to Math.round().
+Rounds to nearest number, with ties rounded towards zero.
+Rounds towards -Infinity. Similar to Math.floor().
+Rounds towards +Infinity. Similar to Math.ceil().
+Rounds towards zero. Similar to Math.trunc().
+Optional
y: Arbitrary-precision decimals. Enables making math calculations with rational numbers, without precision loss.
+1.23(45)
1.23(45)
) can also be parsed back2
and 16
NaN
, Infinity
or -0
.and
, or
, xor
, shiftLeft
, shiftRight
)PI
, sin(1)
.BigInt
type. It automatically switches back and forth between fixed-precision and fractional representations.import { ExactNumber as N } from 'exactnumber';
1 + 0.36 // 1.3599999999999999
N(1).add('0.36').toString() // 1.36
(1 / 49) * 49 // 0.9999999999999999
N(1).div(49).mul(49).toString() // 1
10e16 + 5 // 100000000000000000
N('10e16').add(5).toString() // 100000000000000005
1 / 3 // 0.3333333333333333
N(1).div(3).toString() // 0.(3)
2**32 >> 32 // 0
N(2).pow(32).shiftRight(32).toString() // 1
+
+
+npm i exactnumber
+
+
+It can also be used directly from HTML (via jsDelivr):
+<!-- loads the full, minified library into the global `exactnumber` variable -->
<script src="https://cdn.jsdelivr.net/npm/exactnumber"></script>
<!-- or loads the non-minified library -->
<script src="https://cdn.jsdelivr.net/npm/exactnumber/dist/index.umd.js"></script>
+
+
+import { ExactNumber as N } from 'exactnumber';
N(1).add('3').toString(); // 4
N('1/7').add('1/10').toFraction(); // 17/70
N('1/7').toString(); // 0.(142857)
N('1/7').toString(6); // 0.(05)
N('1/7').toFixed(3); // 0.142
N('1/7').trunc(3).toString(); // 0.142
N('0.(3)').add('0.(6)').toString(); // 1
N('0b1100').bitwiseAnd('0b1010').toString(2); // 1000
N.max('1/1', '10/2', 3).toString(); // 5
N.fromBase('123', 4).toString(); // 27
// approximations
import { PI, sin, pow } from 'exactnumber';
PI(10).toString(); // 3.1415926535
const PI_OVER_2 = PI(10).div(2);
sin(PI_OVER_2, 5).toString(); // 1.00000
// 0.1232323 raised to the power of 2.193333, approximated with 10 decimals
pow('0.1(23)', '2.19(3)', 10).toString(); // 0.0101310867
+
+
+add()
, sub()
mul()
, div()
, divToInt()
pow()
mod()
, powm()
sign()
, abs()
neg()
, inv()
intPart()
, fracPart()
cmp()
, eq()
, lt()
, lte()
, gt()
, gte()
isZero()
, isOne()
isInteger()
round()
, roundToDigits()
, floor()
, ceil()
, trunc()
bitwiseAnd()
, bitwiseOr()
, bitwiseXor()
, shiftLeft()
, shiftRight()
clamp()
getFractionParts()
normalize()
toFixed()
, toExponential()
, toPrecision()
, toString()
, toFraction()
toNumber()
ExactNumber.gcd()
, ExactNumber.lcm()
ExactNumber.min()
, ExactNumber.max()
ExactNumber.fromBase()
ExactNumber.range()
NEAREST_TO_POSITIVE
- Rounds to nearest number, with ties rounded towards +Infinity. Similar to Math.round().
NEAREST_TO_NEGATIVE
- Rounds to nearest number, with ties rounded towards -Infinity.
NEAREST_TO_EVEN
- Rounds to nearest number, with ties rounded towards the nearest even number.
NEAREST_TO_ZERO
- Rounds to nearest number, with ties rounded towards zero.
NEAREST_AWAY_FROM_ZERO
- Rounds to nearest number, with ties rounded away from zero.
TO_POSITIVE
- Rounds towards +Infinity. Similar to Math.ceil().
TO_NEGATIVE
- Rounds towards -Infinity. Similar to Math.floor().
TO_ZERO
- Rounds towards zero. Similar to Math.trunc().
AWAY_FROM_ZERO
- Rounds away from zero
TRUNCATED
FLOORED
EUCLIDEAN
Read more about them here.
+These functions approximate irrational numbers with arbitrary number of decimals. +The last parameter is always used to specify the number of correct decimals in the result.
+sqrt()
, cbrt()
, nthroot()
pow()
, exp()
log()
, logn()
, log10()
, log2()
,PI()
sin()
, cos()
, tan()
asin()
, acos()
, atan()
sinh()
, cosh()
, tanh()
asinh()
, acosh()
, atanh()
License: MIT
+Copyright © 2022 Dani Biró
+Returns the absolute value of this number.
+Returns the sum of this number and the given one.
+Returns the integer bitwise-and combined with another integer.
+Returns the integer bitwise-or combined with another integer.
+Returns the integer bitwise-xor combined with another integer.
+Returns a number which is rounded up to the next largest integer. Same as round(RoundingMode.TO_POSITIVE).
+Optional
decimals: numberReturns number which is clamped to the range delineated by min and max.
+Compares the current number to the provided number. +Returns -1 when the provided number is smaller than the current one. +Returns 0 when the provided number is equal with the current one. +Returns 1 when the provided number is greater than the current one.
+Returns the result of the division of this number by the given one.
+Returns the result of the integer division of this number by the given one. The fractional part is truncated.
+Returns true if the current number is equal to the provided number
+Returns the largest number, but less than or equal to the current number. Same as round(RoundingMode.TO_NEGATIVE).
+Optional
decimals: numberReturns the fractional part of the number (|res| < 1).
+Returns the numerator and the denominator of the current number. +By default it simplifies the fraction
+Optional
normalize: booleanReturns true if the current number is greater than the provided number
+Returns true if the current number is greater than or equal to the provided number
+Returns the integer part of the number.
+Returns the inverse of the number. (1/x)
+Returns true if the current number is less than the provided number
+Returns true if the current number is less than or equal to the provided number
+Optional
type: ModTypeReturns the product of this number and the given one.
+Returns the number with inverted sign. (-x)
+Returns opimized internal representation of the current number (e.g. it simplifies fractions using GCD) +This is may be a slow operation, but in some cases normalization might help with performance of repeated operations.
+Returns this number exponentiated to the given value.
+Returns modulo of this number exponentiated to the given value (modular exponentiation)
+Optional
type: ModTypeRounds current number to the specified amount of decimals. +RoundingMode.NEAREST_TO_POSITIVE is the default
+Optional
decimals: numberOptional
roundingMode: RoundingModeRounds current number to the specified amount of significant digits. +RoundingMode.NEAREST_TO_POSITIVE is the default
+Returns the integer left shifted by a given number of bits.
+Returns the integer right shifted by a given number of bits.
+Returns the difference of this number and the given one.
+Returns a string representing the number in exponential notation. +Defaults to RoundingMode.TO_ZERO
+Optional
roundingMode: RoundingModeOptional
trimZeros: booleanReturns a string representing the number using fixed-point notation, rounded to the specified number of decimals. +Defaults to RoundingMode.TO_ZERO
+Optional
roundingMode: RoundingModeOptional
trimZeros: booleanReturns a string representing the number using fixed-point notation, rounded to the specified number of significant digits. +In contrary to JS Number.toPrecision(), this function never returns exponential notation. +Defaults to RoundingMode.TO_ZERO
+Optional
roundingMode: RoundingModeOptional
trimZeros: booleanReturns string representation in decimal format. +The result might contain repeating digit sequences formatted like "1.4(3)" +It is recommended to set a maximum length for the fractional part to avoid performance issues when having long cycles. +When such limit is provided, toString() truncates the undesired digits
+Optional
radix: numberOptional
maxDigits: numberTruncates the number to the specified number of decimals. Same as round(RoundingMode.TO_ZERO).
+Optional
decimals: number
Rounds away from zero
+