I have some experience using both of these libraries, so I will do my best to explain each of them:
Intl.NumberFormat and decimal.js are both libraries in JavaScript that can be used to format and manipulate numbers. However, while they are similar in some ways, they are very different in others.
Intl.NumberFormat is a built-in JavaScript library that provides localization support for number formatting, including decimal and grouping separators, decimal precision, and currency formatting. It is widely used to format numbers based on a user's language and region.
Decimal.js is a third-party library that provides arbitrary-precision decimal arithmetic for JavaScript. It allows for precise calculations with decimal numbers, which are often required in financial and scientific applications where rounding errors can have significant consequences.
Although Intl.NumberFormat can handle basic currency formatting, it may not be suitable for all use cases. For example, it may not handle more complex currency formatting requirements, such as rounding rules or multiple currencies in the same document. In such cases, decimal.js can provide more precise and flexible currency handling capabilities.
I hope this answered your question!
Answer from Promaster on Stack OverflowI have some experience using both of these libraries, so I will do my best to explain each of them:
Intl.NumberFormat and decimal.js are both libraries in JavaScript that can be used to format and manipulate numbers. However, while they are similar in some ways, they are very different in others.
Intl.NumberFormat is a built-in JavaScript library that provides localization support for number formatting, including decimal and grouping separators, decimal precision, and currency formatting. It is widely used to format numbers based on a user's language and region.
Decimal.js is a third-party library that provides arbitrary-precision decimal arithmetic for JavaScript. It allows for precise calculations with decimal numbers, which are often required in financial and scientific applications where rounding errors can have significant consequences.
Although Intl.NumberFormat can handle basic currency formatting, it may not be suitable for all use cases. For example, it may not handle more complex currency formatting requirements, such as rounding rules or multiple currencies in the same document. In such cases, decimal.js can provide more precise and flexible currency handling capabilities.
I hope this answered your question!
I agree to Promaster's answer, it's also worth noting that using the decimal.js library may have performance implications, as it is a third-party library that requires additional processing overhead compared to the built-in Intl.NumberFormat library. Therefore, you should consider the specific requirements of your application when deciding whether to use decimal.js or Intl.NumberFormat.
The main benefit of using the decimal.js library over Intl.NumberFormat is the ability to perform precise mathematical operations on decimal numbers. If your application requires this level of precision, then decimal.js may be a better choice. However, if your application only requires formatting numbers for display, then Intl.NumberFormat may be sufficient.
» npm install decimal.js
As their author, I recommend bignumber.js or big.js, 'a small, fast Javascript library for arbitrary-precision arithmetic with decimal numbers'.
For a more mature library, the ICU4J BigDecimal translation is also recommended.
There's been a "port" of the Java BigDecimal class (I think it's here: http://freshmeat.net/projects/js_bigdecimal/ ) for a long time. I looked at it a long time ago and it seemed kind-of cumbersome and huge, but (if that's the one I'm thinking of) it's been used as part of some cryptography tools so there's a decent chance that it works OK.
Because cryptography is a likely area to generate a need for such things, that's a good way to snoop around for such packages.
edit: Thanks @Daniel (comment to question) for this older SO question: https://stackoverflow.com/questions/744099/javascript-bigdecimal-library
Hey Node Friends. I'm going to have to build a system that deals with currency and money arithmetic for the first time. We are all aware that using floating points is problematic for these sort of calculations.
Is there a preferred library by the community that deals with this stuff?
The best I've found on my own is big.js
Update: Unlike the comments below, I didn't use float or integers as that is what I was trying not to use in the first place.
mathjs was a suggestion, but that depends on decimal.js anyway so you could just use decimal.js directly.
I read over this comparison between decimal.js, big.js and bignumber.js. I went with big.js in the end, just because it's the more minimal version of the 3 that does everything I need (ex. I only need to use base 10)
Thanks for the help!
https://github.com/Patashu/break_infinity.js
I made this library because decimal.js is very slow, both because it focused on arbitrary precision, and because its functions were not coded with performance in mind to begin with.
By relaxing the arbitrary precision requirement (mantissa is now just a javascript Number, rather than an array of strings, and exp and pow only guarantee about 9 decimal places of accuracy), compared to decimal.js, operations of break_infinity.js are anywhere from 50x to 1000x faster.
How fast is it in practice? Antimatter Dimensions swapped from decimal.js to break_infinity.js in its Eternity Update that came out today ( https://www.reddit.com/r/incremental_games/comments/7grvhl/antimatter_dimensions_eternity_update/ ), and in doing so, the time spent in scripts was reduced to 22% of what it used to be. Obviously if your incremental game is not as math-heavy as Antimatter Dimensions it won't benefit to the same extent, but performance is performance.
Like decimal.js, break_infinity.js handles numbers as large as 1e(9e15). Above that (if you even GET that far), you should use break_break_infinity.js instead, which goes up to 1e(1.79e308), though note that it hasn't been thoroughly tested yet, and obviously it will lose on performance compared to break_infinity.js since it uses a big-integer library for the exponent instead of just a Number.
If you find any bugs or wish to contribute, check out the issues on the github repository ( https://github.com/Patashu/break_infinity.js/issues ) or open a pull request (or message me on Reddit, I guess).