MikeMcl / bignumber.js

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

Issue toNumber() function round up when number is too big (v9.1.1) #346

Closed Sotatek-ThangNguyen2 closed 1 year ago

Sotatek-ThangNguyen2 commented 1 year ago

I have a issue with toNumber() function. I try with BigNumber version: "bignumber.js": "9.1.1",

https://codesandbox.io/s/bignumber-js-forked-sewfsh?file=/src/index.js:0-780

999999999999998400000000000000000000 - 30000000000000000000

Expect Result: 999999999999998370000000000000000000 With toNumber(), same it Rounding Up result: 9.999999999999984e+35 (from 37 -> 40)

screenshot
shuckster commented 1 year ago

This is normal. Your number is way beyond Number.MAX_SAFE_INTEGER.

Use toString instead of toNumber.

Incidentally, JavaScript numbers also exhibit binary floating-point rounding artefacts, which is why we need libraries like bignumber.js in the first place.

Sotatek-ThangNguyen2 commented 1 year ago

@shuckster thanks