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


Use web3's conversion functions fromWei and toWei.

$ npm install web3

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

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

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

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




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.