How to convert token to full token without having any rounding errors.


TypeError: num.isZero is not a function at BN.divmod (node_modules/number-to-bn/node_modules/bn.js/lib/bn.js:2329:17) at BN.div (node_modules/number-to-bn/node_modules/bn.js/lib/bn.js:2425:17) at Promise.all.then () at at process._tickCallback (internal/process/next_tick.js:182:7)

        bal=6221221111111110001/ (10**(18))

It also has some precision errors.

Any help is appreciated.

  • The error-message implies that you are using web3.utils.toBN, not web3.toBigNumber. Please clarify your question. – goodvibration Dec 18 '20 at 8:27
  • And the expression bal=balance/ (10**(18)) means nothing to us when we have no idea what bal and balance are. Please clarify that as well. – goodvibration Dec 18 '20 at 8:28
  • Also 12345 / 10 ** 15 is a value smaller than 1. If you plan to send it to a contract function, then you'll need to convert it to an integer, which means it's going to be a simple 0. So what exactly are you trying to do here? Also, how on earth is that related to bal=balance/ (10**(18)) at the end of the question? – goodvibration Dec 18 '20 at 8:34
  • @goodvibration Sorry for the typo.Updated the question – HF_developer Dec 18 '20 at 8:54
  • What do you mean "without having any rounding errors"? Function web3.utils.toBN.div gives you the integer division, do you want a regular division? You do realize that you will not be able to send the result to a contract function, right? In other words, it will be useful only on your offchain (Javascript) side. – goodvibration Dec 18 '20 at 8:56

First of all, regarding the specific error in your question, change this:


To this:


Or to this:


Second, since class BN supports only integer-arithmetic, the result of your computation is 6.

in order to perform non-integer computation, you will need to use class Decimal instead:

const Decimal = require('decimal.js');
const str = Decimal(6221221111111110001).div(Decimal(10).pow(18)).toFixed();

Or simply:

const str = Decimal('6221221111111110001e-18').toFixed();

Note that you will not be able to send this result to a contract function.

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.