3
public BigInteger getBalance(String address) 
{
    /*Web3j web3j  */

   createEthreumConnection();
   EthGetBalance ethGetBalance = null;
   try {
      ethGetBalance = web.ethGetBalance(address, DefaultBlockParameterName.LATEST).sendAsync().get();
   } catch (InterruptedException e) {               
      logger.error("Interrupted Exception "+e);
   } catch (ExecutionException e) {         
      logger.error("ExecutionException Exception "+e);
   }
   return ethGetBalance.getBalance();
}

I am using the above code for getting the ethereum balance but the balance fetched in given format 4555500000 , I want to be shown as Ethereum balance.For example Bitcoin j provides MonetaryFormat.BTC.noCode().format(addressBalance)

5

@Luiz Soares Thank you for giving me the clue of the value being in WEI but solution provided didn't worked me.After some digging the documentation again got the solution

Convert.fromWei(ethGetBalance.getBalance().toString(), Unit.ETHER);

3

You are getting the value in wei

web3.eth.getBalance returns a BigNumber instance of the current balance for the given address in WEI. https://github.com/ethereum/wiki/wiki/JavaScript-API#returns-33

To converte to Ether: var value = web3.fromWei('21000000000000', 'ether');

https://github.com/ethereum/wiki/wiki/JavaScript-API#web3fromwei

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.