Ok, I realize it’s early on a Sunday so I hope I’m just missing something obvious:
I have this function:
private decimal CashConversion(decimal amount, decimal ratio)
{
if (ratio == 1) return amount;
decimal convertedAmount = amount / ratio;
return Math.Round(convertedAmount, 2);
}
When I call it like this:
decimal tax = CashConversion(96.53, 15);
The “tax” variable is equal to 6.43. However, 96.53/15 is 6.435333333333333. Rounding that to 2 places should return 6.44. Am I missing something here?
Check the documentation for Math.Round: As 2 is even, and the next digit after the second is 5, the value is rounded down, according to the IEEE Standard 754, section 4. It’s called banker’s rounding.
It’s not an error, but an expected behaviour. Maybe not the one you were expecting though.
If you want the “mathematically correct” behaviour though, you can call the Decimal.Round(Decimal, Int32, MidpointRounding) overload, as in: