MikeMcl / bignumber.js

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

shiftedBy unexpected output #312

Closed blake256 closed 2 years ago

blake256 commented 2 years ago

Hello @MikeMcl

I'm not sure if this is intended behavior or not but the following conversion returns an unexpected value:

new BigNumber(1.111111111111111111).shiftedBy(18).toFixed()

1111111111111111200 = actual 1111111111111111111 = expected

  1. Maybe I'm missing some rounding configuration option or something else?
  2. shiftedBy(18) is the same as times(new BigNumber(10).pow(18)) correct?
shuckster commented 2 years ago

One of the reasons APIs like BigNumber exist is to avoid the problems of the native JavaScript number type, which you've provided as input.

To fix it, use a string instead:

new BigNumber('1.111111111111111111').shiftedBy(18).toFixed()
// "1111111111111111111"
blake256 commented 2 years ago

One of the reasons APIs like BigNumber exist is to avoid the problems of the native JavaScript number type, which you've provided as input.

To fix it, use a string instead:

new BigNumber('1.111111111111111111').shiftedBy(18).toFixed()
// "1111111111111111111"

Good point, thank you very much ser!