currency.js icon indicating copy to clipboard operation
currency.js copied to clipboard

Decimal value will be changed when value is unsafe float number

Open kpaxqin opened this issue 4 years ago • 1 comments

Example: image

See: https://codesandbox.io/s/objective-hodgkin-4h7p0?file=/src/index.js

This is because the native Number cannot present 70368744177664.01 correctly (Which has been discussed in stackoverflow), and currency.js use native Number to precise float up to int value.

image

The Readme.md & https://currency.js.org/ says

As long as your currencies are less than 253 (in cents) or 90,071,992,547,409.91, you should not see any problems.

But in this case it seems not working as expect.

kpaxqin avatar Aug 18 '20 10:08 kpaxqin

This is something I overlooked since the conversion to int is done after the number has been represented as a decimal value, which as you mentioned cannot be safely represented due to precision limitations. I will need to dig into this further to see if there's a way I can address this.

In the meantime, you should be able to use the newly added fromCents option to safely display large values:

currency(7036874417766401, { fromCents: true }).format(); // "70368744177664.01"

scurker avatar Sep 16 '20 01:09 scurker