1

As JavaScript has its quirks with floats (e.g. 0.1+0.2 is > 0.3 and similarly in divisions), how to deal with currency conversion? Usually currency conversion rates are in float going from 6 and more decimal points. Converting them from one currency to another and to another, seems highly prone to javascript float errors. While currency conversion is quite common task, I wonder how do people usually deal with this?

For now, I don't have any solution and not sure if it has caused issues in the past. Even if I had this issue, it is likely to be missed. I simply use whatever currency rates I have and multiply/divide to get the other currency. This is how I work with currencies at the moment

  1. Get the currency exchange rates for USD from a 3rd party API. Sample response { base: 'USD', rates: { INR: 80.8653858542, GBP: ... } }
  2. I assume a dummy currency XXX to have a constant relationship with INR e.g. 1 XXX = 200 INR. This rate never changes. I chose INR because this is my bank account currency. I use the dummy currency to make sure that I can always show a predictable amount to users irrespective of the exchange rates.
  3. I use this dummy currency as a base currency. So whatever rates I received for USD from step 1, I convert it into XXX i.e. usdToXXX = usdToInr/200 = 80.8653858542/200 = 0.404326929271 and finally the rate card becomes ` { base: XXX, rates: { INR: 200, USD: 0.404326929271 }
  4. Now for all the conversions, I use this rate card of XXX
fossmoat
  • 43
  • 6
  • After you multiply by the conversion rate, round the number to the appopriate number of decimal digits. – Barmar Feb 27 '23 at 01:33
  • how do I choose that appropriate number for rounding? and even if I do that, wouldn't the float calculations lead to unexpected output? – fossmoat Feb 27 '23 at 01:36
  • If it's US dollars, the appropriate number is 2 digits, since that's the smallest amount. – Barmar Feb 27 '23 at 01:38
  • 1
    This isn't a JavaScript problem, the same is true of every language. See https://stackoverflow.com/questions/588004/why-does-floating-point-arithmetic-not-give-exact-results-when-adding-decimal-fr or https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – user229044 Feb 27 '23 at 01:38
  • Anyway, don't worry about the error. It's very small, and rounding will discard it. – Barmar Feb 27 '23 at 01:39
  • https://stackoverflow.com/questions/588004/why-does-floating-point-arithmetic-not-give-exact-results-when-adding-decimal-fr – epascarello Feb 27 '23 at 02:14
  • A simple trick for certain currencies is to keep the value to integers by its minimal accepted value. For instance, treating `$12.56` as `1256 cents` for calculation and put back the decimal points after rounding. – Bumhan Yu Feb 27 '23 at 03:48
  • Handling monetary values becomes very tricky very quickly. If for production purposes, using a proven library like [Dinero.js](https://dinerojs.com/) would be safer. If for learning purposes, I'd still recommend checking out `Dinero.js` to see how they handle calculation cases. – Bumhan Yu Feb 27 '23 at 03:51
  • Before you can get answers, you need to figure out what the problem is. Your question does not state any problem to be solved. What issues can there be? If you have some amount of one currency and convert it to some amount of another currency, is it a problem if the result differs from the result that real-number arithmetic would have produced by some tiny amount of money? E.g., if you get $12,345.56 instead of $12,345.57, is that a problem? Why, and for whom? Can you fix it by just giving customers an extra penny in every transaction where the conversion is not exact? – Eric Postpischil Feb 27 '23 at 11:46
  • Do you need to be able to convert to a currency and back and get the original number every time? Why? What functions is your software supposed to perform with these currency conversions, and what are the issues involved if the conversions are off slightly? – Eric Postpischil Feb 27 '23 at 11:47
  • @eric-postpischil I know some cases where this is issue and I believe I have blind spot in catching the errors that could be caused. Which is why, I am looking to learn how people are dealing with currencies in general, afterall this is a common use case for every business that accepts international payments. – fossmoat Mar 10 '23 at 02:28
  • Specific issue that I can already predict - convert the currency to user's local currency to show them how much it costs them but charging their card on a different currency(the one which my bank account supports). I can see these two values will differ by significant amount (note: the payment charged is in the range of $500-10000). – fossmoat Mar 10 '23 at 02:34

1 Answers1

-1

You can use an "arbitrary precision decimal/float" library for JavaScript - find one that is suitable for your situation.

As far as I'm aware, the way these work is by storing the numbers as a different representation and using a different algorithm - similar to a technique you would use to add/subtract/multiply/divide numbers "by hand"

While in contrast, a primitive numeric type - integers, floats etc are using the processor directly - so have issues of overflow, precision errors etc