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

[Support] Using BigNumber.js for blockchain app #314

Closed quangnv13 closed 2 years ago

quangnv13 commented 2 years ago

Hello everyone, now i'm using BigNumber.js for frontend of my blockchain app. It's work perfectly but some use case with uint256 value: 603563865116404668245589250390106310780736245694817307641463070622848318552 and use code bellow: new BigNumber(603563865116404668245589250390106310780736245694817307641463070622848318552).toString(10) return value is 603563865116404600000000000000000000000000000000000000000000000000000000000. I need input number with big value and return origin value as string.

Someone can help me solution for this?

shuckster commented 2 years ago

That number is being invisibly transformed by the JavaScript runtime before it becomes an input to BigNumber because it is above Number.MAX_SAFE_INTEGER.

Your inputs to BigNumber should be strings only.

Try to avoid using the native number-type completely, because it has more problems than just this one.

quangnv13 commented 2 years ago

That number is being invisibly transformed by the JavaScript runtime to the output you are seeing because it is above Number.MAX_SAFE_INTEGER.

Your inputs to BigNumber should be strings only.

Try to avoid using the native number-type completely, because it has more problems than just this one.

Thanks for reply

MikeMcl commented 2 years ago

Yes, and unless you want the value rounded to the current DECIMAL_PLACES and ROUNDING_MODE settings, use the more efficient toString() rather than toString(10). See toString.