4 ms·
It's true that representing currency in floating-point vars is never going to be 100% accurate - but for price quotations, estimates and convenience, you can ge
by josscrowcroft 15y ago
It's true that representing currency in floating-point vars is never going to be 100% accurate - but for price quotations, estimates and convenience, you can get pretty accurate results (provided you have accurate exchange rates)
Remember of course that any currency conversion will always carry inaccuracy, because every time money changes currency, somebody takes a cut.
I recommend using this alongside some other numbers-related library (such as accounting.js) to fix binary value rounding issues (and also for decent formatting)
Out of interest, what do you consider to be "accurate conversions on the server side"?
- RodgerTheGreat 15y agoIf I had to do accurate calculations from scratch, I would probably used fixed-point or arbitrary-precision math, talk to an accountant about industry standards and carefully cover my edge-cases for rounding and the like. More practically I would try to use a well-established library. I can't recommend anything specifically. It's not impossible to do accurate calculations in JS, but my main point is that since the only built-in way to do math is floating point it's a bit of a minefield to go with a naive solution.
- josscrowcroft 15y agoFair play. I definitely wouldn't feel good recommending money.js for any application where people live and die by the 8th point of precision on an exchange rate, if you know what I mean - but it's a solid solution for estimates, price conversions and most enterprise software - provided that appropriate disclaimers are given (i.e. "converted prices for informational purposes only") - everyone should cover their arses. I agree with you though, that there will be the 1% of cases where something a lot more reliable is needed, but I imagine that's when you call in the CS heavyweights.