bigNumber not parsing ether properly
Asked Answered
Y

1

7

I have the following:

import { BigNumber } from "@ethersproject/bignumber";
import { parseUnits } from "@ethersproject/units";

const decimals = 18;

export const add = (a: string, b: string): string => {
  const _a = parseUnits(a, decimals);
  console.log(_a.toString(), a);
  const _b = BigNumber.from(b);
  const res = _a.add(_b).toString();
  return res;
};

// a = 123456789123456789.123456789123456789
// b = 1
// _a.toString() = 123456789123456789123456789123456789
// res = 123456789123456789123456789123456790

Am I missing something obvious as to why res wouldn't be calculated as "123456789123456790.123456789123456789"?

Even if I don't pass in decimals, it's still the same result. (Ideally I wouldn't want to specify an actual decimals value)

Yellowweed answered 28/5, 2021 at 10:0 Comment(0)
S
15

The BigNumber is actually a BigInteger (you can't have decimal values in it). This is because on the Ethereum blockchain, numbers are represented as 256 bit integers. You can also see my answer to the same topic.

Now working with decimals could be confusing let me put it this way:

// creating BigNumber instances
const a = ethers.BigNumber.from('1') // creates BigNumber of 1
const b = ethers.utils.parseUnits('1', 18) // Creates BigNumber of 1000000000000000000
const c = ethers.utils.parseEther('1') // same as above, by default 18 decimals

a.toString() // '1'
b.toString() // '1000000000000000000'
ethers.utils.formatEther(a) // '0.000000000000000001'
ethers.utils.formatEther(b) // '1.0'

If you're mostly dealing with currency numerics having decimals, then you can simply stick with parseEther and formatEther utils.

If you're not working with currency and it's still a BigNumber then you'll can use BigNumber.from() and value.toString()

Soprano answered 30/5, 2021 at 10:33 Comment(1)
can you explain why if we're not working with currency that we can use BigNumber.from()? For instance if I try to ethers.BigNumber.from("21.33") it fails. I haven't figured out why yetSquarely

© 2022 - 2024 — McMap. All rights reserved.