Convert string to decimal with precision in web3 fromWei method

Viewed 2151

I have a value 169482670391774 and I know that it should be converted to decimal and has 9 digits after comma, so in this example value after conversion should be 169482.670391774.

How to convert this value?

What I have so far is:

let mapping = web3.utils.unitMap;

let valuePrecision = "";
for (var i = 0; i < accuracy; i++) {
   valuePrecision = valuePrecision + "0";
}
let unitMapValue = 1 + valuePrecision;
let unitMapKey = '';

for (const key in mapping) {
 if (mapping[key] === unitMapValue) {
  unitMapKey = key;
 }
}

let inTxnsInToken = web3.utils.fromWei(web3.utils.toBN(inTxns).toString(), unitMapKey);
let outTxnsInToken = web3.utils.fromWei(web3.utils.toBN(outTxns).toString(), unitMapKey)

And this is working but I am feeling that this is not the best method and I can't use:

web3.utils.toBN(inTxns).toString(), 1000000000)

0 Answers
Related