bignumber.js

repository·main·Indexed 27 days ago

https://github.com/mikemcl/bignumber.js

A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic, version 11.1.5. It provides immutable BigNumber instances for high-precision calculations, supporting operations such as addition, subtraction, multiplication, division, and square root. The library includes features for global configuration of decimal places and rounding modes, internationalization formatting via .toFormat(), and conversion to fractions.

Tokens
2.1K
Snippets
15
Records
17
Agent score
42%

What's inside bignumber.js

  1. Minify the bignumber.js browser bundle

    main

    You can minify the browser/global bundle using terser. First, install terser globally, then run the minification command against the distribution file.

    npm install -g terser
    terser dist/bignumber.js -c -m -o dist/bignumber.min.js
  2. Load bignumber.js in the Browser

    main

    You can load bignumber.js in a browser using a standard <script> tag or as an ES module via a CDN.

    Standard Script Tag:

    <script src='https://cdn.jsdelivr.net/npm/bignumber.js@latest/dist/bignumber.min.js'></script>

    ES Module:

    <script type="module">
    import BigNumber from 'https://cdn.jsdelivr.net/npm/bignumber.js@latest/+esm';
    // ...
    </script>
  3. Run tests for bignumber.js

    main

    The test suite is located in the test/methods directory. Before running tests, you must build the CommonJS distributable. You can run all tests using npm test or by calling node test/test. To test a specific method, run its corresponding script directly, for example: node test/methods/toFraction. For browser-based testing, open test/test.html in a web browser.

    npm run build
    npm test
    # or: node test/test
    
    # To test a single method:
    node test/methods/toFraction
  4. Use bigtime-OOM.js for detailed performance profiling

    main

    The bigtime-OOM.js tool is a variation of bigtime.js designed for more granular timing. Unlike bigtime.js, it provides separate timings for object creation versus method execution.

    Warning: This tool creates objects in a single batch and is prone to running out of memory (OOM) if the number of iterations is very high (e.g., > 500,000) or the number of random digits is large (e.g., > 40).

  5. Load bignumber.js in Node.js (CommonJS and ESM)

    main

    Depending on your module system, import BigNumber as follows:

    CommonJS:

    const BigNumber = require('bignumber.js');

    ES Modules:

    import BigNumber from 'bignumber.js';
    // or
    import { BigNumber } from 'bignumber.js';
  6. Verify TypeScript type declarations

    main
    TypeScript compilation tests are located in the test/typescript directory. These tests verify that type declarations and imports work correctly across different module formats. Run the type-checking suite using npm run typecheck.
    npm run typecheck
  7. Compare bignumber.js performance with bigdecimal.js using bigtime.js

    main

    Use the bigtime.js command-line application to compare the performance of bignumber.js methods against JavaScript translations of Java's BigDecimal (GWT or ICU4J versions). This tool runs a specified number of iterations using random operands and verifies that the results match.

    To run a comparison, use the following syntax:

    node bigtime <method> <iterations> <digits>

    Example: To time 10,000 calls to the plus method using operands with up to 40 random digits:

    node bigtime plus 10000 40
    node bigtime plus 10000 40
  8. Load bignumber.js in Deno

    main

    In Deno, import the ES module directly from a URL. It is recommended to use @deno-types to provide type definitions.

    // @deno-types="https://raw.githubusercontent.com/MikeMcl/bignumber.js/main/dist/bignumber.d.mts"
    import BigNumber from 'https://raw.githubusercontent.com/MikeMcl/bignumber.js/main/dist/bignumber.mjs';
  9. Configure Global BigNumber Settings

    main

    Use BigNumber.set() to configure global settings such as DECIMAL_PLACES and ROUNDING_MODE. These settings affect the precision of operations like division, square root, and base conversion.

    BigNumber.set({ DECIMAL_PLACES: 10, ROUNDING_MODE: 4 });
    
    let x = new BigNumber(2);
    let y = new BigNumber(3);
    let z = x.dividedBy(y); // "0.6666666667"
    BigNumber.set({ DECIMAL_PLACES: 10, ROUNDING_MODE: 4 })
  10. Create Independent BigNumber Constructors

    main

    For advanced usage, you can create a new constructor with its own independent configuration using BigNumber.clone(). This allows different parts of your application to use different precision settings.

    BigNumber.set({ DECIMAL_PLACES: 10 });
    const BN = BigNumber.clone({ DECIMAL_PLACES: 5 });
    
    let x = new BigNumber(1);
    let y = new BN(1);
    
    x.div(3); // '0.3333333333'
    y.div(3); // '0.33333'
    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'
  11. Perform Arithmetic with BigNumber

    main

    BigNumber instances are immutable; methods return a new BigNumber rather than modifying the original. Methods can be chained for complex calculations.

    Common Arithmetic Methods:

    • .plus(n) / .add(n)
    • .minus(n) / .sub(n)
    • .times(n) / .mul(n)
    • .dividedBy(n) / .div(n)
    • .exponentiatedBy(n) / .pow(n)
    • .squareRoot() / .sqrt()
    • .modulo(n) / .mod(n)

    Example:

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