Skip to content
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

Support of scientific notation #209

Open
Kukunin opened this issue Dec 5, 2018 · 7 comments
Open

Support of scientific notation #209

Kukunin opened this issue Dec 5, 2018 · 7 comments

Comments

@Kukunin
Copy link

Kukunin commented Dec 5, 2018

I believe that bn.js should support scientific notation for big numbers: a number 5e+21 or a string "5e+21" or both.

Current situation:

new BN('500000000000000000000').toString() // works well
new BN('5e+21').toString() // returns 63521 for some reason
new BN(500000000000000000000).toString() // throws Error: Assertion failed
new BN(5e+21).toString() // throws Error: Assertion failed

I don't know the reasons behind the assertion of big numbers, but at least it can support scientific notation as a string

@nblogist
Copy link

@Kukunin Having same issue 2 years later, what did you do to solve this?

Thanks!

@nblogist
Copy link

For anyone getting the same issue, apparently BN.js throws from 1e+21 and above, I solved it using the following function:

function toPlainString(num) { // BN.js Throws from 1e+21 and above so using this make shift function
					return (''+num).replace(/(-?)(\d*)\.?(\d+)e([+-]\d+)/,
					  function(a,b,c,d,e) {
						return e < 0
						  ? b + '0.' + Array(1-e-c.length).join(0) + c + d
						  : b + c + d + Array(e-d.length+1).join(0);
					  });
				  }

@qalqi
Copy link

qalqi commented Mar 28, 2021

Need fix for this

@samlaf
Copy link

samlaf commented Jul 14, 2022

I would also like to be able to do BigNumber.from('1e27').

@nblogist
Copy link

@samlaf this did the job for me 2 years back, is it not working now?

#209 (comment)

@june07
Copy link

june07 commented Jul 16, 2022

new BN(${Intl.NumberFormat('en-US').format(1e+99).replaceAll(',','')})

<BN: 1d42aea2879f2e44dea5a13ae3465277b06749ce90c777839e74404a7e8000000000000000000000000>

@samlaf
Copy link

samlaf commented Jul 19, 2022

Nice @june07 , but still think this should be hidden inside the library. Why do I need this ugly code in my user code?

github-merge-queue bot pushed a commit to MetaMask/metamask-mobile that referenced this issue Mar 13, 2025
## **Description**

- Unpin and dedupe `ethereumjs-util` and `ethereumjs-abi` while staying
on legacy v6
- Replace `import { BN } from 'ethereumjs-util'` with importing `BN`
explicitly from either `bn.js@4` or `bn.js@5`
  - Mixing the two is not safe and can cause breakage.
- [Some on why this is A Bad
Thing](ethereumjs/ethereumjs-util#250)
- We don't get complete and correct typings with importing the old
re-export
- This adds both v4 and v5 as explicit dependencies - ideally v4 will be
removed as transition to v5 concludes
- Add EIP-1191 address checksum algorithm support for
`toChecksumAddress()`
- Unblock relying on the ancient and deprecated `[email protected]`
  
- [`ethereumjs-util`
changelog](https://github.com/ethereumjs/ethereumjs-util/blob/master/CHANGELOG.md)
-
[`[email protected]`](ethereumjs/ethereumjs-util@v6.1.0...v6.2.0)

## **Related issues**

- indutny/bn.js#209
- indutny/bn.js#295
- #12010

#### Blocking
- #11932
  - #12043
  - #11968

## **Manual testing steps**


## **Screenshots/Recordings**

### **Before**

### **After**

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile
Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.

---------

Co-authored-by: Nicolas MASSART <[email protected]>
Co-authored-by: Nico MASSART <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants