2

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.InvariantCulture);
}

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!

Gaz
  • 3,855
  • 6
  • 28
  • 33
  • possible duplicate of [decimal rounding is off for (276/304)*304](http://stackoverflow.com/questions/5029955/decimal-rounding-is-off-for-276-304304) – Daniel Hilgarth Mar 29 '11 at 08:54

1 Answers1

4

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).

dipdapdop
  • 126
  • 1
  • 10
Andrew Skirrow
  • 3,402
  • 18
  • 41