13

I'm building an ecommerce site integrated with paypal.

We take multiple currencies, so I want to make sure that (for accounting reasons) i'm correctly performing any math for currency conversion.

After multiplying the currency conversion * the original currency, I always end up with lots of trailing numbers after the decimal point.

Is there a standard way to do this? Should I truncate or round? Do I need to round multiple times in case rounding the 1/1000 decimal will affect rounding the 1/100 decimal?

Should I be doing something like:

Math.Round(Math.Round(x, 3), 2)

I've been having trouble finding good information about how this is done (hopefully US and Europe are the same).

oleksii
  • 35,458
  • 16
  • 93
  • 163
Ralph N
  • 4,240
  • 8
  • 28
  • 36

3 Answers3

7

You should use Math.Round(x, 2, MidpointRounding.AwayFromZero)

Patrick McDonald
  • 64,141
  • 14
  • 108
  • 120
  • This isn't the "bankers rounding" mentioned above... any thoughts? – Ralph N Jan 03 '12 at 23:12
  • I suppose since you're doing the currency conversion this could arguably be better that you guys would ALWAYS get some fraction of a cent that wouldn't have existed. – Chris Marisic Jan 04 '12 at 14:39
  • Well... this was the CLOSEST to the answer. After speaking with the accountant, supposedly the trick is to always round up from 5 when calculating your taxes/VAT. That way, you are always sure/safe that you paid your taxes. Slightly different topic from original post - but my question is answered. – Ralph N Jan 17 '12 at 00:43
  • 1
    It's the way I was taught rounding in school and, as I never (unnecessarily) use rounded values in intermediate calculations I'm not worried about a bias in my rounding, I can't bring myself to use the default rounding method in .NET. Note also that .ToString("0.00") uses this method, as does display rounding in Excel. – Patrick McDonald Jan 17 '12 at 11:56
  • 1
    This only works correctly for currencies with 2 decimals right of the decimal point, line EUR and USD. There are many currencies which are different, like Algeria, Iraq, Kuwait and a few other Middle Eastern nations, using 3 decimals, Bitcoin uses 8. Chtiwi Malek's answer works for any currency. – DhyMik Sep 16 '21 at 11:31
3

if your site has multiple currencies then use this to correctly round decimals according to the current user's currency and culture :

int currencyDecs = System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalDigits;
decimal roundedDecimalPrice = Math.Round(decimalPrice, currencyDecs, MidpointRounding.AwayFromZero);
Chtioui Malek
  • 11,197
  • 1
  • 72
  • 69
3

I would assume you should just be able to directly use Math.Round as .NET uses Bankers Rounding.

Just remember to always make rounding be the absolute very last thing you do to not introduce rounding errors.

In a currency system like this, I would also store the real unrounded values as decimals additionally for future problem solving purposes.

Community
  • 1
  • 1
Chris Marisic
  • 32,487
  • 24
  • 164
  • 258