Simple issue but couldn't find solution. When I run a console to interact with a function that returns uint in a smart contract, it shows in hexadecimal notation. Like this:

<BN: a> (meaning the value 10 in decimal)

How do I specify the output format so I can see the value in decimal? I don't think it's necessary for that matter, but the coding is as follows:

uint dist;
function getDist() public view returns (uint distancia){
    return dist;

You haven't shared your JavaScript code, but presumably you're just logging the BN directly? Try calling .toString() on whatever variable holds the BN.

  • I've found that .toString() takes a parameter, that's why it wasn't working for me before. Thanks. – Tadeu Moreira Jan 13 at 15:43
  • What version of web3.js are you using, and what parameter did you need to pass to .toString()? – smarx Jan 13 at 15:52
  • web3.js 1.0.0 uses this bn.js, where you should be able to call .toString() without any parameters to get the default behavior of base 10 with no extra padding: github.com/indutny/bn.js/blob/master/lib/bn.js#L447 – smarx Jan 13 at 15:55
  • '1.0.0-beta.37'. The parameter was the representation desired. 10 for decimal. So I did: contractInstance.getDist().then(function(x){console.log(x.toString(10))}) and it worked – Tadeu Moreira Jan 13 at 15:55
  • You're right, 10 is default and so it works too. Thanks again – Tadeu Moreira Jan 13 at 15:57

I don't think returning decimal from Solidity function is supported at the moment. The reason is using hex-decimal saves more space than decimal when transmitting data. The conversation from hex to other formats like string or decimal needs to happen at front-end either manually or via a js library.

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.