I can just use web3's fromWei function for all token that have 18 decimal places but I'm not sure how to convert token to full token without having to worry about rounding errors.

For example convert a token with 15 decimals:

convert 12345 to 0.000000000012345 and so on.

Any help is appreciated

2 Answers 2


Using a library like BigNumber or BN is important in JavaScript to avoid rounding errors in the floating point representation.

Using web3.js:

> web3.toBigNumber(12345).div(10**15).toFixed()
< "0.000000000012345"

Note that when you call a smart contract from JavaScript, you'll typically get a BigNumber object back in the first place, so you can start there (e.g. result.div(10**15).toFixed()).


With a limit on decimals, rounding errors can exist.

A token with 18 decimals can potentially (and does) have rounding errors.

The EVM takes the floor, or rounds down, to the nearest whole number.

Knowing this ahead of time, developers can do math calculations appropriately and accurately.

// assuming the usage of `new BN()` - pseudocode below
To convert 1 token to 18 decimals: (1.mul(1e18)) = 1000000000000000000
To convert 1 token to 15 decimals: (1.mul(1e15)) = 1000000000000000

To convert 12345 (assuming smallest unit) to decimal representation (15): (12345.div(1e15)) = 0.000000000012345

Personally, I like to handle the smallest subunit of a token as a whole unit in javascript. Then I can take the floor of my result and trust that's how the EVM will handle the calculation.


Token decimals: 15

User A Balance: 3.5 tokens

Actual balance as represented by uint256: 3500000000000000 <- this is the number I use

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.