MikeMcl / bignumber.js

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

420517031.05749343 #266

Closed zhaiyb closed 4 years ago

zhaiyb commented 4 years ago

BigNumber(420517031.05749343).toString() === "420517031.05749345" 420517031.05749346 has the same problem

MikeMcl commented 4 years ago

There is no JavaScript number 420517031.05749343 in the same way as there is no JavaScript number 9.99999999999999999999999999999999.

Open a console in your browser and type in any number literal, and then press Enter to see the JavaScript number that results

>> 420517031.05749343
420517031.05749345

>> 9.99999999999999999999999999999999
10

Due to the limited precision of JavaScript numbers (64 bits), if you are passing a number literal with more than 15 significant digits to the BigNumber constructor, then you must pass it as a string to ensure it results in the expected value.

BigNumber("420517031.05749343").toString() === "420517031.05749343"    // true