How to use the default function from bignumber.js

Find comprehensive JavaScript bignumber.js.default code examples handpicked from public code repositorys.

bignumber.js.default is a JavaScript library that provides support for arbitrary-precision decimal and non-decimal arithmetic in JavaScript.

120
121
122
123
124
125
126
127
128
129
    const index = data.columns.findIndex(col => col.key === key);
    // statisticsRow[key] = {
    //   formula: `SUM(${getArea(index, index + 1, recordRowStart, recordRowEnd)})`,
    //   result: BigNumber.sum(...records.map(r => new BigNumber((r[key] as any) || 0))).toNumber()
    // }
    statisticsRow[key] = bignumber_js_1.default.sum(...records.map(r => new bignumber_js_1.default(r[key] || 0))).toNumber();
}
if (statistics.length > 0) {
    worksheet.addRow(statisticsRow, 'i'); // 添加Footer row
}
fork icon0
star icon0
watch icon2

+ 9 other calls in file

How does bignumber.js.default work?

bignumber.js.default provides a constructor function that creates a new BigNumber object. This object can represent numbers with arbitrary precision, beyond what is supported by the built-in JavaScript Number type. BigNumber objects can be used to perform arithmetic operations, such as addition, subtraction, multiplication, and division, on numbers with arbitrary precision. These operations can be performed on BigNumber objects, or between BigNumber objects and standard JavaScript Number values. The library also provides various utility functions, such as isBigNumber and isFinite, to check the type and value of BigNumber objects. bignumber.js.default supports a range of formatting options, including support for scientific notation, trailing zeros, and custom rounding modes. The library is compatible with CommonJS, AMD, and browser environments, and is available as an npm package for use in Node.js projects.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const BigNumber = require("bignumber.js");

// Create new BigNumber objects
const a = new BigNumber("123456789012345678901234567890");
const b = new BigNumber("987654321098765432109876543210");

// Perform arithmetic operations with arbitrary-precision numbers
const sum = a.plus(b);
const difference = a.minus(b);
const product = a.times(b);
const quotient = a.dividedBy(b);

// Output the results
console.log("sum:", sum.toString());
console.log("difference:", difference.toString());
console.log("product:", product.toString());
console.log("quotient:", quotient.toString());

In this example, we first import the bignumber.js library using the require function and use it to create two BigNumber objects with arbitrary-precision decimal values. We then perform arithmetic operations on these BigNumber objects using the plus, minus, times, and dividedBy methods, and store the results in new BigNumber objects. Finally, we use the toString method to convert these BigNumber objects to strings, and output the results to the console. Because BigNumber objects can represent numbers with arbitrary precision, we can use bignumber.js to perform arithmetic operations on numbers that would otherwise be too large or too small to be accurately represented using the built-in JavaScript Number type.