I'm converting currencies and have encountered a strange rounding issue when using entity framework. Here's the abbreviated code:
var v = select new ValuationsModel
{
LocalValue = dv.LocalValue,
CurrencyId = dv.CurrencyId,
FXRate = fx.ExchangeRate,
USDValue = dv.LocalValue * (dv.CurrencyId == "USD" ? 1.0m : Convert.ToDecimal(fx.ExchangeRate)),
}).ToList();
decimal usdValue = v.LocalValue.Value * (v.CurrencyId == "USD" ? 1.0m : Convert.ToDecimal(v.FXRate));
Here are the values (all were verified in the quick watch window):
fx.ExchangeRate
is type Double
dv.CurrencyId
is type String
dv.LocalValue
is type nullable Decimal
fx.ExchangeRate = 1.36678
dv.CurrencyId = "GBP"
dv.LocalValue = 15102141.1994
The value in USDValue
is: 20689933.443178 = 15102141.1994 * 1.37
The value in usdValue
is: 20641304.548515932 = 15102141.1994 * 1.36678
Why would fx.ExchangeRate
get rounded to 1.37 in the select new
statement but not be rounded in the exact same calculation below? I even copied the currency check into the second assignment to make sure that wasn't affecting things. Do I just have to do the calculation outside of the select new
statement if I want accuracy?