Conversion of OracleDecimal to System.Double in C#
I'm having some problems with the conversion of value from an OracleDecimal. Here is the code
public static T GetReaderValue(this OracleDataReader dr, string col) where T : struct
{
int colNo = dr.GetOrdinal(col);
var value = (double)OracleDecimal.SetPrecision(dr.GetOracleDecimal(colNo), 28);
return (T) Convert.ChangeType(value, typeof(T), CultureInfo.Invarian开发者_StackOverflow中文版tCulture);
}
This works fine for most values but for some, like 0.12345, it returns numbers like 0.123499999999.
Can anyone offer some advise on how to convert OracleDecimal without these rounding errors?
Thanks!
System.Double
is stored in base 2 rather than base 10. Some numbers that can be represented using a finite number of digits in base 10 require an infinite number of digits in base 2 (and vice-versa).
As the database appears to be storing a decimal number you might be better off converting to a System.Decimal
value instead so you don't lose precision (as System.Decimal
uses base 10 in the same manner as OracleDecimal
).
精彩评论