Solve the problem of js number arithmetic precision-bignumber.js

A JavaScript library for arbitrary precision decimal and non-decimal arithmetic

Address: https://github.com/MikeMcl/bignumber.js

Features

  •  Integers and decimals
  •  Simple API, but full-featured
  • Faster, smaller, and possibly easier to use than the JavaScript version of Java's BigDecimal
  •  Only 8 KB in size 
  • Support password security pseudo-random number generation
  • No dependency
  • Broad platform compatibility: only use JavaScript 1.5 (ECMAScript 3) features

 Browser:

<script src='path/to/bignumber.js'></script>

Node.js

​​​​​​​$ npm install bignumber.js
const BigNumber = require('bignumber.js');
import BigNumber from "bignumber.js";
// or
import { BigNumber } from "bignumber.js";

 use

A single constructor BigNumber, which accepts a value of type Number, String or BigNumber,

let x = new BigNumber(123.4567);
let y = BigNumber('123456.7e-3');
let z = new BigNumber(x);
x.isEqualTo(y) && y.isEqualTo(z) && x.isEqualTo(z);      // true

 

To get the string value of BigNumber, you can use toString () or toFixed (). Using toFixed() can prevent returning to exponential notation, no matter how large or small the value is.

let x = new BigNumber('1111222233334444555566');
x.toString();                       // "1.111222233334444555566e+21"
x.toFixed();                        // "1111222233334444555566"

 

If you do not have a good understanding of the limited precision of Number values, it is recommended to create BigNumbers from String values ​​instead of Number values ​​to avoid possible loss of precision.

In all further examples below, semicolons and toString calls are not shown. If the commented out value is in quotation marks, it means that toString was called on the previous expression.

// Precision loss from using numeric literals with more than 15 significant digits.
new BigNumber(1.0000000000000001)         // '1'
new BigNumber(88259496234518.57)          // '88259496234518.56'
new BigNumber(99999999999999999999)       // '100000000000000000000'

// Precision loss from using numeric literals outside the range of Number values.
new BigNumber(2e+308)                     // 'Infinity'
new BigNumber(1e-324)                     // '0'

// Precision loss from the unexpected result of arithmetic with Number values.
new BigNumber(0.7 + 0.1)                  // '0.7999999999999999'

 

When creating BigNumber from Number, please note that BigNumber is created from the decimal toString() value of Number, not from its underlying binary value. If the latter is required, pass the toString (2) value of Number and specify base 2.

new BigNumber(Number.MAX_VALUE.toString(2), 2)

 

 

BigNumbers can be created from base values ​​from 2 to 36. See ALPHABET to expand this range.

a = new BigNumber(1011, 2)          // "11"
b = new BigNumber('zz.9', 36)       // "1295.25"
c = a.plus(b)                       // "1306.25"

 

 

If you do not specify base 10 for decimal values, the performance will be better. Specify base 10 only if you want to limit the number of decimal places of the input value to the current DECIMAL _ places setting.

 

BigNumber is immutable because its methods will not change it.

0.3 - 0.1                           // 0.19999999999999998
x = new BigNumber(0.3)
x.minus(0.1)                        // "0.2"
x                                   // "0.3"

 

 

Methods that return BigNumber can be chained together.

x.dividedBy(y).plus(z).times(9)
x.times('1.23456780123456789e+9').plus(9876.5432321).dividedBy('4444562598.111772').integerValue()

 

 

Some longer method names have shorter aliases.

x.squareRoot().dividedBy(y).exponentiatedBy(3).isEqualTo(x.sqrt().div(y).pow(3))    // true
x.modulo(y).multipliedBy(z).eq(x.mod(y).times(z))                                   // true

 

 

Like JavaScript's Number type, there are toExponential, toFixed and toPrecision methods.

x = new BigNumber(255.5)
x.toExponential(5)                  // "2.55500e+2"
x.toFixed(5)                        // "255.50000"
x.toPrecision(5)                    // "255.50"
x.toNumber()                        //  255.5

 

 

You can specify the base for toString.

 

If the base 10 is not specified, the performance will be better, that is, use toString () instead of toString (10). Specify base 10 only if you want to limit the number of decimal places of the string to the current DECIMAL _ places setting.

x.toString(16)                     // "ff.8"

 

 

There is a toFormat method that may be useful for internationalization.

y = new BigNumber('1234567.898765')
y.toFormat(2)                       // "1,234,567.90"

 

 

Use the set or config method of the BigNumber constructor to set the maximum number of decimal places involved in the operation result of division (ie division, square root, base conversion, or negative power operation).

 

Other arithmetic operations always give precise results.

BigNumber.set({ DECIMAL_PLACES: 10, ROUNDING_MODE: 4 })

x = new BigNumber(2)
y = new BigNumber(3)
z = x.dividedBy(y)                        // "0.6666666667"
z.squareRoot()                            // "0.8164965809"
z.exponentiatedBy(-3)                     // "3.3749999995"
z.toString(2)                             // "0.1010101011"
z.multipliedBy(z)                         // "0.44444444448888888889"
z.multipliedBy(z).decimalPlaces(10)       // "0.4444444445"

 

 

There is a toFraction method with an optional maximum denominator parameter

y = new BigNumber(355)
pi = y.dividedBy(113)               // "3.1415929204"
pi.toFraction()                     // [ "7853982301", "2500000000" ]
pi.toFraction(1000)                 // [ "355", "113" ]

 

 

And isNaN and isFinite methods, because NaN and Infinity are valid BigNumber values.

x = new BigNumber(NaN)                                           // "NaN"
y = new BigNumber(Infinity)                                      // "Infinity"
x.isNaN() && !y.isNaN() && !x.isFinite() && !y.isFinite()        // true

 

 

The value of BigNumber is stored in decimal floating point format with coefficients, exponents, and signs.

x = new BigNumber(-123.456);
x.c                                 // [ 123, 45600000000000 ]  coefficient (i.e. significand)
x.e                                 // 2                        exponent
x.s                                 // -1                       sign

 

 

For advanced usage, you can create multiple BigNumber constructors, each with its own independent configuration.

// Set DECIMAL_PLACES for the original BigNumber constructor
BigNumber.set({ DECIMAL_PLACES: 10 })

// Create another BigNumber constructor, optionally passing in a configuration object
BN = BigNumber.clone({ DECIMAL_PLACES: 5 })

x = new BigNumber(1)
y = new BN(1)

x.div(3)                            // '0.3333333333'
y.div(3)                            // '0.33333'

 

 

To avoid calling toString or valueOf on BigNumber to get its value in Node.js REPL, or use it when using console.log

BigNumber.prototype[require('util').inspect.custom] = BigNumber.prototype.valueOf;

 

 

 

 

Guess you like

Origin blog.csdn.net/liuhao9999/article/details/113698908