I am using ethers.js I can't figure out how do you convert a bignumber like 1252500000000000000 to 125.25



I was able to use ethers.utils.formatEther( value ) ⇒ string to convert back to readable.

  • 1
    Notice that this assumes you are formatting a number with 18 decimals (like eth itself, or most ERC20 tokens). If you wanted to format a token with a different number of decimals (like USDC, which has 6), you need to use ethers.utils.formatUnits(value, 6). formatEther is a shorthand for formatUnits(value, 18). Jun 10 '21 at 12:53
  • I have a number that shows as 512 in the etherscan. 0.000000000000000512
    – dcsan
    Oct 31 '21 at 19:21
  • current the above returns a tiny number, is there a way to convert a uInt? now i'm doing this: Math.round(parseFloat(str) * (10 ** 18))
    – dcsan
    Oct 31 '21 at 19:22
const BigNumber = require('bignumber.js');

let num=new BigNumber(1252500000000000000)
let denom = new BigNumber(10).pow(16)
let ans = num.dividedBy(denom).toNumber()

Hope this explains your question.For further info,refer https://mikemcl.github.io/bignumber.js/

  • This returns NaN
    – LiveWire
    Jun 5 '21 at 4:23
  • 1
    I use web3js for above snippet.Sorry i didn't notice etherjs in your question earlier
    – kappa
    Jun 5 '21 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.