5

I am new to testing and I want to know how to convert a big number to a real number.

I use bignumber in the following command:

console.log(await this.crowdsale.rate());

This is what gets printed in the terminal:

BigNumber { s: 1, e: 2, c: [ 500 ] }

2 Answers 2

4

It depends what you mean by real number. You can check out here the divisions used in Ethereum.

For wei:

let bigNo = await this.crowdsale.rate();
console.log(bigNo.toNumber());

For ether add this:

console.log(web3.fromWei(bigNo.toNumber()));

If you're using version ^1.0.0 of web3, use web3.utils.fromWei instead of web3.fromWei.

1
  • 1
    thanks paul .i forgot about toNumber() thanks for reminding me about it .
    – AVATAR
    Commented Sep 25, 2018 at 15:20
3

Not every BigNumber can be converted into a Javascript Number with an equivalent value.

If that had been the case, then we wouldn't have needed a BigNumber class to begin with.

Referring to your actual question - you haven't really explained why you need to perform this conversion.

Your question implies that you don't like the printout, in which case you can fix your code as follows:

let rate = await this.crowdsale.rate();
console.log(rate.toFixed());

If you're interested in performing additional calculations, then you can simply use BigNumber functions.

So technically speaking, there shouldn't be any need convert your BigNumber into a Javascript Number.

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.