MikeMcl / bignumber.js

A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic
http://mikemcl.github.io/bignumber.js
MIT License
6.69k stars 742 forks source link

Very large numbers are stringified in scientific notation #209

Closed cemremengu closed 5 years ago

cemremengu commented 5 years ago
const BigNumber = require("bignumber.js")
console.log(BigNumber('5000310000187400001874').toString())

Expected: "5000310000187400001874" Actual: "5.000310000187400001874e+21"

https://runkit.com/cemremengu/5c10eb8866628400131646bc

cemremengu commented 5 years ago

My bad, needed to use toPrecision. Sorry for the noise.

cemremengu commented 5 years ago

Although docs seem to be incorrect for toPrecision

If sd is omitted, or is null or undefined, then the return value is the same as n.toString().

which is not the case

MikeMcl commented 5 years ago

To avoid scientific notation then either use toFixed() or change the value of EXPONENTIAL_AT:

const n = new BigNumber('5000310000187400001874');
console.log(n.toFixed());     //  "5000310000187400001874"
console.log(n.toString());    //  "5.000310000187400001874e+21"
BigNumber.set({EXPONENTIAL_AT: 25});
console.log(n.toString());    //  "5000310000187400001874"

There is no reason to use toPrecision without an argument, but in doing so you have discovered a mismatch between the documentation and behaviour. I will fix this presently. Thanks for the report.