How to use the sum function from bignumber.js

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

The bignumber.js.sum function returns the sum of two or more BigNumber instances.

115
116
117
118
119
120
121
122
123
124
if (tokenId == null) {
  const map = {};
  result.forEach(info => {
    const address = info.holder;
    if (map[address] === undefined) map[address] = 0;
    map[address] = BigNumber.sum(new BigNumber(map[address]), new BigNumber(info.amount)).toFixed();
  })
  holders = Object.keys(map).map(address => ({
    address: address,
    amount: map[address]
fork icon24
star icon58
watch icon23

+ 7 other calls in file

891
892
893
894
895
896
897
898
899
// 值数量
const length = nums.length
if (length) {

    // 合计
    let num = BigNumber.sum(...nums)

    // 平均值 = 总值 / 数量
    num = num.dividedBy(length)
fork icon0
star icon2
watch icon2

+ 17 other calls in file

How does bignumber.js.sum work?

bignumber.js library's sum method is used to add multiple BigNumber values and return their sum as a new BigNumber object.

When adding the values, the method ensures that the maximum precision of the inputs is used and that any carryover from previous additions is taken into account.

The method can also handle values of different signs and can return a result with a specific number of significant digits and/or decimal places.

Ai Example

1
2
3
4
5
6
7
8
const BigNumber = require("bignumber.js");

const num1 = new BigNumber("123456789");
const num2 = new BigNumber("987654321");

const result = num1.plus(num2);

console.log(result.toString()); // "1111111110"

In this example, we first import the bignumber.js library and create two BigNumber instances with the values 123456789 and 987654321. We then use the plus method to add these two values together, and store the result in a new BigNumber instance called result. Finally, we log the result as a string using the toString method.