0

Hello i am trying to convert unit256 format depending on the decimal value of each token address.

Decimal 9 and 18 ı did converted. they work fine as seen in the code below

but I don't know what to do for decimal 8 and 4. Can you help me with this?

exports.gweiToEth = (val, dec) => {
if (dec === "9") {
    return converter.convert(val, "gwei", "ether")
}
else if (dec === "18") {
    return converter.convert(val, "wei", "ether")
}
return val }

I would appreciate it if you share it by updating my code, thank you very much in advance

0

Use web3's conversion functions fromWei and toWei.

$ npm install web3



var Web3 = require('web3');
console.log(Web3.version);
// => 1.0.0-beta.34

const weiValue = Web3.utils.toWei('1', 'ether');
console.log(weiValue);
// => 1000000000000000000

const etherValue = Web3.utils.fromWei('1000000000000000000', 'ether');
console.log(etherValue);
// => 1

An alternative is to just understand how it works mathematically and make your own function. Refer to the links below.

References:

https://web3js.readthedocs.io/en/v1.2.11/web3-utils.html

https://medium.com/@piyopiyo/how-to-convert-ether-from-wei-and-vice-versa-with-web3-1-0-0-3e3e691e3f0e

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.