[Open source tripartite library] bignumber.js: a big number math library

The OpenHarmony (OpenAtom OpenHarmony "OpenHarmony" for short) tripartite library is a software component that has been verified to be reusable on the OpenHarmony system and can help developers quickly develop OpenHarmony applications. If it is released to the open source community, it is called an open source tripartite library, and developers can obtain it by visiting the open source community. Next, let's learn about bignumber.js open source tripartite library.

bignumber.js is a math library, JavaScript library for arbitrary precision decimal and non-decimal arithmetic.

features

  • Integer sum decimal.
  • Simple API, but full-featured.
  • Faster, smaller, and perhaps easier to use than Java's BigDecimal JavaScript version.
  • 8 KB minified and compressed.
  • Copy JavaScript's Number type's , and methods toExponential, toFixed, toPrecision, toString.
  • Includes a method toFraction squareRoot that rounds correctly.
  • Supports cryptographically secure pseudo-random number generation.
  • No dependencies.
  • Broad platform compatibility: uses only JavaScript 1.5 (ECMAScript 3) features.

scenes to be used

Because of the calculation accuracy problem in JavaScript, direct calculation may lead to various bugs. To solve this problem, you can use the BigNumber.js library. Its general principle is to treat all numbers as strings and reimplement the calculation logic.

Example of use

  1. Install bignumber.js

ohpm install bignumber.js

OpenHarmony ohpm  environment configuration and more, please refer to How to install OpenHarmony ohpm package

  1. import bignumber.js

import BigNumber from "bignumber.js";

  1. call interface

let x = new BigNumber(123.4567);

let y = BigNumber('123456.7e-3');

let z = new BigNumber(x);

expect(x).assertEqual(y);

expect(y).assertEqual(z);

expect(x).assertEqual(z);

The library exports a single constructor BigNumber that accepts a value of type Number, String or BigNumber.

let x = new BigNumber('1111222233334444555566');

x.toString();                       // "1.111222233334444555566e+21"

x.toFixed();                        // "1111222233334444555566"

To get the string value of a BigNumber, use toString() or toFixed().

// Using numeric literals with more than 15 significant digits results in a loss of precision.

new BigNumber(1.0000000000000001)         // '1'

new BigNumber(88259496234518.57)          // '88259496234518.56'

new BigNumber(99999999999999999999)       // '100000000000000000000'

// Using numeric literals outside the numeric range will result in a loss of precision.

new BigNumber(2e+308)                     // 'Infinity'

new BigNumber(1e-324)                     // '0'

//An unexpected result of an arithmetic operation on a Number value resulted in a loss of precision.

new BigNumber(0.7 + 0.1)                  // '0.7999999999999999'

If you are not well aware of the limited precision of numeric values, it is recommended to create BigNumbers from string values ​​rather than numeric values ​​to avoid potential loss of precision.

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

       When creating large numbers from numbers, be aware that large numbers are created from the decimal value of the number, not from its underlying binary value. If the latter is desired, pass the value of Number and specify the base 2.toString() or toString(2).

a = new BigNumber(1011, 2)          // "11"

b = new BigNumber('zz.9', 36)       // "1295.25"

c = a.plus(b)   

Large numbers can be created from values ​​in bases 2 to 36. See ALPHABET for an extension on this.

0.3 - 0.1                           // 0.19999999999999998

x = new BigNumber(0.3)

x.minus(0.1)                        // "0.2"

x                                   // "0.3"

Better performance if base 10 is not specified for decimal values. Specify base 10 only if you want to limit the number of decimal places in the input value to the current INK http://mik setting. BigNumber is immutable because it cannot be changed by its methods.

x.dividedBy(y).plus(z).times(9)

x.times('1.23456780123456789e+9').plus(9876.5432321).dividedBy('4444562598.111772').integerValue() // "0.3"  

Methods that return BigNumber can be chained.

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

Like JavaScript's Number type, BigNumber has toExponential, toFixed, and toPrecision methods.

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

A radix can be specified for toString.

Performance is better if no base 10 is specified, i.e. use toString() instead of toString(10). If you want to limit the number of decimal places in the string to the current DECIMAL_PLACES setting, specify only base 10.

y = new BigNumber('1234567.898765')

y.toFormat(2)                       // "1,234,567.90"

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

y = new BigNumber(355)

pi = y.dividedBy(113)               // "3.1415929204"

pi.toFraction()                     // [ "7853982301", "2500000000" ]

pi.toFraction(1000)                 // [ "355", "113" ]

There is a toFraction method which takes an optional maximum denominator parameter.

x = new BigNumber(NaN)                                           // "NaN"

y = new BigNumber(Infinity)                                      // "Infinity"

x.isNaN() && !y.isNaN() && !x.isFinite() && !y.isFinite()

and the isNaN and isFinite methods, since and are valid values. NaN, Infinity, BigNumber.

x = new BigNumber(-123.456);

x.c                                 // [ 123, 45600000000000 ]  coefficient (i.e. significand)

x.e                                 // 2                        exponent

x.s                                 // -1                       sign

The value of BigNumber is stored in decimal floating-point format with coefficient, exponent, and sign.

// Set DECIMAL_LACES 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'

For advanced use, multiple BigNumber constructors can be created, each with its own independent configuration.

Source code address & ohpm warehouse address

The above are some basic introductions of bignumber.js. For more details, please refer to the bignumber.js document at the source address or ohpm warehouse address.

References

How to install the OpenHarmony ohpm package :

bignumber.js API:

Source address:

ohpm address:

Click to follow and read the original text for more information

Guess you like

Origin blog.csdn.net/OpenHarmony_dev/article/details/132402449