2

I am using Etherscan API to retrieve certain erc20 token balance on given ETH address and get a number like 62910000000 without decimals separator. In fact the proper balance is 6291000.0000. How to fix it? Or maybe how can I display it in a correct way?

1 Answer 1

2

Most ERC20 tokens should follow the pattern of using 18 decimal places for their token. If your token is using a special value, you can either hard code that, or use other APIs to retrieve that decimals value for the token.

The Etherscan API is returning you the value without that in consideration, but all you really need to do is divide that number by 1018 (or add the decimal place in the right spot using string manipulation).

For example this account has 418.98 in BNB.

The api query returns "418940000000000000000".

If you divide by 1018 you will get the "real" token balance. Note that you will need to use some sort of "big number" library to do this kind of math, or as I mentioned, string manipulation:

> a.slice(0,a.length - 18) + "." + a.slice(a.length - 18, a.length)
"418.940000000000000000"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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