-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Discussion: Support scientific notation in BigNumbers? #228
Comments
I think it's not necessary and might just confuse people. There is already stuff like For instance, here are two great functions from the 0x wrapper: https://github.com/0xProject/0x-monorepo/blob/development/packages/web3-wrapper/src/web3_wrapper.ts /**
* A unit amount is defined as the amount of a token above the specified decimal places (integer part).
* E.g: If a currency has 18 decimal places, 1e18 or one quintillion of the currency is equivalent
* to 1 unit.
* @param amount The amount in baseUnits that you would like converted to units.
* @param decimals The number of decimal places the unit amount has.
* @return The amount in units.
*/
public static toUnitAmount(amount: BigNumber, decimals: number): BigNumber {
assert.isValidBaseUnitAmount('amount', amount);
assert.isNumber('decimals', decimals);
const aUnit = new BigNumber(BASE_TEN).pow(decimals);
const unit = amount.div(aUnit);
return unit;
}
/**
* A baseUnit is defined as the smallest denomination of a token. An amount expressed in baseUnits
* is the amount expressed in the smallest denomination.
* E.g: 1 unit of a token with 18 decimal places is expressed in baseUnits as 1000000000000000000
* @param amount The amount of units that you would like converted to baseUnits.
* @param decimals The number of decimal places the unit amount has.
* @return The amount in baseUnits.
*/
public static toBaseUnitAmount(amount: BigNumber, decimals: number): BigNumber {
assert.isBigNumber('amount', amount);
assert.isNumber('decimals', decimals);
const unit = new BigNumber(BASE_TEN).pow(decimals);
const baseUnitAmount = amount.times(unit);
const hasDecimals = baseUnitAmount.decimalPlaces() !== 0;
if (hasDecimals) {
throw new Error(`Invalid unit amount: ${amount.toString()} - Too many decimal places`);
}
return baseUnitAmount;
} I think these kinds of methods with some extra functionality like common exponentiations solve a lot of the use cases. One thing I think would be great is a |
I agree with you, but someone asked, so I thought I'd put it up for discussion. I like to get consensus and not just make broad-sweeping decisions. :) For your random number, you could use:
Or for salts, which are usually just bytes32:
:) |
Wow.... that is too easy. Thank you. |
I think we have reached a consensus (an my personal preference) to not support scientific notation in the BigNumber class. Closing this issue, but if anyone wants to re-open it, please feel free to do so. :) |
It might be worthwhile to add support for scientific notation as it is currently being recommended in the "URL Format for Transaction Requests" ERC681 format.
Depending on the adoption of the specification it would be great to easily convert the value using the https://github.com/ethereum/EIPs/blob/master/EIPS/eip-681.md |
Excellent point. :) I’ll look more into that, thanks. (Re-opening) |
(The above commit reference is wrong; it was meant to address #301) |
Any news on the adoption of EIP-681? I think this might make sense to add as a static method to the new FixedNumber object regardless, rather than allow scientific notation in arbitrary places. |
Also, as I try to implement this I notice the specification has unbalanced brackets... :s I'm guessing:
was meant to be something like:
but it still doesn't fully make sense to me. For now I'll ignore units. It will be backwards compatible once the spec is corrected. |
Ah - it is possible to prevent exponentiation of bignumber.js by calling |
Looks like this was related to ethers-io/ethers.js#228, but we just need to force the bignumber string to not be in scientific notation when transforming the value, and then this works as expected. Note that the outputted value in the outputted transaction will **not** be in scientific notation. Closes #7466
Looks like this was related to ethers-io/ethers.js#228, but we just need to force the bignumber string to not be in scientific notation when transforming the value, and then this works as expected. Note that the outputted value in the outputted transaction will **not** be in scientific notation. Closes #7466
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
This came up recently, and so I thought I would open it up for discussion.
Pros:
ethers.constants.WeiPerEther
, but other common values may come up too, such as1e9
referring to wei per gwei.Cons:
1e18
is 7704 in hex); this may be less an issue, since hexadecimal strings must begin with a0x
prefix, and currently this is already a problem if people try19
, meaning0x19
, which is 25bigNumberify((1e18 + 65) - (1e18 + 64))
; the underflow occurs before we have a chance to catch it; this is already still an issue thoughAnyways, please anyone else please feel free to add, augment or refute any points.
The text was updated successfully, but these errors were encountered: