This article can be seen as a translation of the official bignumber.js documentation.

Bignumber.js is a library for performing mathematical operations with arbitrary precision. The API can be tested on the console of the official website

constructor

/*
     * n {number|string|BigNumber} A numeric value.
     * [b] {number} The base of n. Integer, 2 to ALPHABET.length inclusive.
     */
    function BigNumber(n, b) {
        
    }
Copy the code

A static method

clone()

Generates a separate BigNumber constructor

Clone () BN(1).div(3).tonumber () //Copy the code

config()

Set the parameters for the standalone BigNumber constructor

The parameters are as follows:

  1. DECIMAL_PLACES(default: 20) Is used to set decimal places. In division, in the square root, in exponents.
Clone () BN. Config ({DECIMAL_PLACES:4}) BN(1).div(3).tonumber () //0.3333Copy the code
  1. ROUNDING_MODE(default value: 4) Indicates the rounding mode. For details, see the documentation
// Value range: bignumber. ROUND_UP = 0; ROUND_DOWN = 1; // Round bignumber. ROUND_DOWN = 1; ROUND_CEIL = 2; // Round bignumber. ROUND_CEIL = 2; // round to positive infinite BigNumber.ROUND_FLOOR = 3; ROUND_HALF_UP = 4; ROUND_HALF_UP = 4; // Round: Round to the nearest number, or up if the distance between two adjacent numbers is equal. BigNumber.ROUND_HALF_DOWN = 5; // Round to the nearest number, or down if the distance between two adjacent numbers is equal. BigNumber.ROUND_HALF_EVEN = 6; ROUND_HALF_CEIL = 7 ROUND_HALF_CEIL = 7 ROUND_HALF_CEIL = 7 ROUND_HALF_CEIL = 7 BigNumber.ROUND_HALF_FLOOR = 8;Copy the code
  1. EXPONENTIAL_AT(default value is [-7,20])
  2. RANGE(default [-1e+9,1e+9])
  3. CRYPTO(default false) is used to set the random generation algorithm for bignumber.random (). If this cannot be set to true, math.random () is used to generate a random value.
  4. MODULO_MODE(default: ROUND_DOWN) Mode for collecting modules
  5. POW_PRECISION(Default: 0) Precision of poW operation results
  6. FORMATE(formatting corresponding Settings)
Default value: BigNumber. Config (). The FORMAT = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = {decimalSeparator:"."
fractionGroupSeparator: ""
fractionGroupSize: 0
groupSeparator: ","
groupSize: 3
secondaryGroupSize: 0
}
Copy the code

maximum([]),minimum([])

Gets the maximum/minimum value in the array

random([precision])

Returns a pseudo-random value that can specify the number of decimal points

Instance methods

Addition:. Plus (n [, base]

0.1 + 0.2 // 0.30000000000000004 x = new BigNumber(0.1) y = x.price (0.2) //'0.3'BigNumber (0.7). Plus (x). Plus (y) / /'1'
x.plus('0.1'/ /, 8)'0.225'
Copy the code

Tail [, base

0.3-0.1 // 0.19999999999999998 x = new BigNumber(0.3) x.minus(0.1) //'0.2'/ / x.m inus (0.6, 20)'0'
Copy the code

Multiplication:. Times (n [, base]) BigNumber; M. ultipliedby (n [, Base])

0.6 * 3 // 1.7999999999999998 x = new BigNumber(0.6) y = x.moltipliedby (3) //'1.8'
BigNumber('7e+500').times(y)    // '1.26 e+501'
x.multipliedBy('-a'/ /, 16)'- 6Copy the code

division

“. Div (n [, base]) BigNumber; DividedBy (n [, base]

x = new BigNumber(355)
y = new BigNumber(113)
x.dividedBy(y)                  // '3.14159292035398230088'
x.div(5)                        // '71'
x.div(47, 16)                   // '5'
Copy the code

Note: The division calculation is rounded based on DECIMAL_PLACES and ROUNDING_MODE properties.

Division, return integer:.idiv(n [, base]) BigNumber; DividedToIntegerByv (n [, base]

x = new BigNumber(355)
y = new BigNumber(113)
x.dividedBy(y)                  // '3.14159292035398230088'
x.div(5)                        // '71'
x.div(47, 16)                   // '5'
Copy the code

Tail (n [, base]) BigNumber; Modulo.(n [, base]

X = new BigNumber(1) x.modulo(0.9) //'0.1'
y = new BigNumber(33)
y.mod('a'/ /, 33)'3'
Copy the code

Note: Mod/mod operations are affected by the MODULO_MODE setting

Index arithmetic:.pow(n [, m]) BigNumber; ExponentiatedBy (n [, M]

Math.pow(0.7, 2) // 0.4899999999999999994 x = new BigNumber(0.7) x.exponentiatedby (2) //'0.49'
BigNumber(3).pow(-2)            // '0.11111111111111111111'
Copy the code

Note: n stands for exponent. If n<0, the result is definitely a decimal, so the result is affected by DECIMAL_PLACES and ROUNDING_MODE.

Square:. SQRT () . SquareRoot. () ⇒ BigNumber

x = new BigNumber(16)
x.squareRoot()                  / / '4'
y = new BigNumber(3)
y.sqrt()                        / / '1.73205080756887729353'
Copy the code

.a. ComparedTo (n [, base]

Refer to the following table for comparison results:

1 operand > n-1 operand <n 0 operand ==n Null operand or n is not a numberCopy the code

For example:

x = new BigNumber(Infinity) y = new BigNumber(5) x.comparedTo(y) // 1 x.comparedTo(x.minus(1)) // 0 y.comparedTo(NaN) //  null y.comparedTo('110'/ / - 1, 2)Copy the code

Precision adjustment

Dp ([dp [, rm]]) ⇒ BigNumber | number; DecimalPlaces. [dp [, rm]]) ⇒ BigNumber | number according to dp (decimals places) and rm (round mode) for rounding the operands

X = new BigNumber(1234.56) x.decimalPlaces(1) //'1234.6'X.dp () // 2 If dp is not passed, the function returns the number of decimal places of the operand. X.decimalplaces (2) //'1234.56'
x.dp(10)                               // '1234.56'
x.decimalPlaces(0, 1)                  // '1234'
x.dp(0, 6)                             // '1235'
x.decimalPlaces(1, 1)                  // '1234.5'
x.dp(1, BigNumber.ROUND_HALF_EVEN)     // '1234.6'
x                                      // '1234.56'
Copy the code

Integer:. IntegerValue ([rm]) BigNumber

Returns the integer part of the operand according to round mode (RM)

x = new BigNumber(123.456)
x.integerValue()                        / / '123'
x.integerValue(BigNumber.ROUND_CEIL)    / / '124'
y = new BigNumber(12.7)
y.integerValue()                        // '-13'
y.integerValue(BigNumber.ROUND_DOWN)    // '-12', rounded to 0
Copy the code

Significant figures. Sd ([, rm] [d]) ⇒ BigNumber | number; Precision. ([[d, rm]]) ⇒ BigNumber | number

What is a significant number? The number from the first non-zero digit to the last digit is called a significant digit. For example, there are three significant digits of 0.618, namely 6,1,8.

X = new BigNumber(9876.54321) x.recision (6) //'9876.54'X. sd() // 9 If d is omitted, the number of valid digits x. recision(6, bignumber.round_up) // is returned'9876.55'
x.sd(2)                                // '9900' //todo
x.precision(2, 1)                      // '9800'    //todo
y = new BigNumber(987000)
y.precision()                          // 3
y.sd(true) // 6, such as multi-parameter sdtrue, the 0 following the integer part is also considered a significant numberCopy the code

. ToFixed ([DP [, RM]]

Returns a string of decimal bits truncated or filled according to dp. Note: Unlike Number.prototype.toFixed, which returns exponential notation if a number is greater or equal to 1021, this method will always return normal notation.

Number. Prototype. toFixed(DP

  1. If dp is omitted, BigNumber(1.123).tofixed () returns ‘1.123’ as is; 1.123.tofixed () removes all decimal places and returns only the integer bit ‘1’
  2. If the operand is greater than 10^21, BigNumber(10^21).tofixed () is returned as a normal number; 10^21. ToFixed () will be returned in exponential form
Math.h pow (10, 20). ToFixed () / /"100000000000000000000"Math.h pow (10, 21). ToFixed () / /"1e+21"

BigNumber(10).pow(20).toFixed() //"100000000000000000000"
BigNumber(10).pow(21).toFixed() //"1000000000000000000000"
Copy the code

Example:

X = 3.456 y = new BigNumber(x) x.fixed () //'3'
y.toFixed()                     // '3.456'
y.toFixed(0)                    // '3'
x.toFixed(2)                    // '3.46'
y.toFixed(2)                    // '3.46'
y.toFixed(2, 1)                 // '3.45'  (ROUND_DOWN)
x.toFixed(5)                    // '3.45600'
y.toFixed(5)                    // '3.45600'
Copy the code

Note: The main difference between toFixed() and dp() is that toFixed returns a string, which is filled with zeros if there are not enough decimal places; Dp () method returns the BigNumber | number, small digital enough to ignore

ToFormat ([dp [, rm]]) string

Returns a string, rounded according to dp and rm, and formatted according to the FORMAT attribute.

format = {
    decimalSeparator: '. ',
    groupSeparator: ', ',
    groupSize: 3,
    secondaryGroupSize: 0,
    fractionGroupSeparator: ' ',
    fractionGroupSize: 0
}
BigNumber.config({ FORMAT: format })

x = new BigNumber('123456789.123456789')
x.toFormat()                    // '123456789123567 89'
x.toFormat(1)                   // '123456789 us'
Copy the code

Convert to js base numeric type.tonumber ()

The effect is the same as using the + operator

X = new BigNumber(456.789) x.tonumber () // 456.789 +x // 456.789Copy the code