0

I am fairly new to this and please let me know if I am wrong in any point.

I am trying to convert WEI into Eth, without using the Web3 library.

const weiValue = 100000;
const ethValue = ethers.utils.formatEther(weiValue);<--- answer is correct at 0.0000000000001

My code requires me to change 40000000000000000 wei to eth.

and here is what I have:

const weiValue = 40000000000000000;
const tokenRateString = tokenRate.toString();
const ethValue = ethers.utils.formatEther();

I believe I need to convert the Wei into a string as it too large to be represented accurately as integers in JS

I am confused on what is the intermediate step here

TylerH
  • 20,799
  • 66
  • 75
  • 101

1 Answers1

2

To who ever comes here later.

Yes, a big number such like this : 40000000000000000 need to be a string.

What you need to do is this: 40000000000000000

const  WeiToEth = (tokenRate)=> {
 const weiValue = BigNumber.from("40000000000000000");
 const ethValue = ethers.utils.formatEther(weiValue);
 console.log(ethValue) }

It converts the number into a string

Here is the documentation : https://docs.ethers.io/v5/api/utils/bignumber/

TylerH
  • 20,799
  • 66
  • 75
  • 101