I have written an ERC20 token contract and deployed it in test network using truffle framework. When I check the token balance for an account using web3 it was giving the result in BigNumber when I try to convert it to a number using toNumber() getting an unexpected result.

Solidity code:

  function balanceOf(address _from) constant public returns (uint256 balance) {
    return balances[_from];

Javascript code:

instantiateBalance() {
const contract = require('truffle-contract')
const token = contract(Token)
var tokenInstance;
token.deployed().then(function(instance) {
tokenInstance = instance;
return tokenInstance.balanceOf.call("0xb1Cf866ced575FD1A1997Daa5d6F099efb282E41", {from: "0xb1Cf866ced575FD1A1997Daa5d6F099efb282E41"});
}).then(function(balance) {

Expected Result

Decimals = 18

Actual Token Balance = 100000000.000000000000000000

Actual Result

Token Balance = 1000000000000


  • 1
    did you try balance.div(1e18) or balance.toNumber().div(1e18) ?
    – Rajesh
    Commented Oct 31, 2017 at 5:16
  • balance.div(1e18) will give me the digits before the decimal point. I need digits after decimal point also to display exact balance. Commented Oct 31, 2017 at 6:37

5 Answers 5


If your token have 18 decimals

like an ether you can use

then(function(balance) {
    console.log(web3.fromWei(balance.toNumber(), "ether" ) );

Use fromWei to convert your bigNumber to ether and then convert it to Number. Be aware that unnecessary 0 after the floating point will not be displayed.

In web3 ^1.0 it may change to web3.utils.fromWei instead of web3.fromWei doc. Note that truffle ^5.0 uses web3 ^1.0.

If you have a different token decimal

Let's say 6. You'll have to do the conversion manually with something like:

var tokenBalance = balance.ToNumber() / Math.pow(10, tokenDecimals)
  • 1
    I experienced a problem with this example, if the balance had 18 decimals it would strip it back to 16. To fix this I used web3.fromWei(balance.toString(), "ether"), notice .toString(). I'm unsure if this is best practice. Commented Feb 21, 2018 at 14:53
  • yes using toString() worked for me as well, when I used toNumber(), it gave me an error because we can not store that big number in that data type I think. Commented Dec 24, 2019 at 18:25
  • ToNumber not a function in truffle Commented Sep 7, 2020 at 17:40
  • @PhilipRego this was answered 3 years ago. It's obvious it changed now. Commented Sep 9, 2020 at 15:19
  • From Etherjs BigNumber.from(bn).div(BigNumber.from(10).pow(decimal)).toNumber()
    – Sedhu
    Commented Dec 16, 2021 at 5:15

You can use exponential number syntax in javascript to shorten you code.

Example (18 decimals):

const balance = getBalance().toNumber() / 1E18

PS. Sorry, i can not make comment due to low reputation.


Personally, I like to work with big numbers and not parse them into "normal" numbers. I guess I'm afraid some overflow may happen if I don't.

Therefore, if a contract returns a uint, then I parse the return value to a BigNumber (from bignumber.js), like so:

const balanceBefore = new BigNumber(await contract.getBalance(...));

and I like to use the BigNumber API for my assertions, like so:

  • If you -1 please also add a comment with the reason why ... thank you. Commented Sep 8, 2020 at 10:06

A fast way could be

import web3 from 'web3';

const ethProvider = new web3(web3.givenProvider).eth;
const accounts = await ethProvider.requestAccounts();
const ethBalance = web3.utils.fromWei(await ethProvider.getBalance(accounts[0]), 'ether');

I believe you just want log the balance, you should use



It's throws genuine error that it can't fit the number in js number type.

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.