0

I have a strange problem on getting data from DB

I use C# and Oracle.DataAccess with stored procedure.

In the DB I have a NUMBER(10,2) type field named TotalPayout and when dumping it on the DB I get this data

select a.totalpayout, DUMP(a.totalpayout)    from   tbla a    where a.totalpayout > 17 and a.totalpayout < 18;

returns:

TOTALPAYOUT , DUMP(A.TOTALPAYOUT)

  • 17.40 , Typ=2 Len=3: 193,18,41

  • 17.50 , Typ=2 Len=3: 193,18,51

  • 17.60 , Typ=2 Len=3: 193,18,61

  • 17.70 , Typ=2 Len=3: 193,18,71

But when running the progrem on the DataSet into column type Double I get this values

Total Payout

  • 17.400000000000002
  • 17.5

  • 17.7

  • 17.6

The first value get extra 00000000000002 Can’t find any clue why this happening.

this is the code i use to get the data

  public static DataSet ExecuteDataset(OracleConnection connection, CommandType commandType, string commandText, params OracleParameter[] commandParameters)
    {
        if (connection == null) throw new ArgumentNullException("connection");

        // Create a command and prepare it for execution
        OracleCommand cmd = new OracleCommand();
        bool mustCloseConnection = false;
        PrepareCommand(cmd, connection, (OracleTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);

        // Create the DataAdapter & DataSet
        using (OracleDataAdapter da = new OracleDataAdapter(cmd))
        {
            DataSet ds = new DataSet();
            ds.Locale = CultureInfo.InvariantCulture;

            // Fill the DataSet using default values for DataTable names, etc
            da.Fill(ds);

            // Detach the OracleParameters from the command object, so they can be used again
            cmd.Parameters.Clear();

            if (mustCloseConnection)
                connection.Close();

            // Return the dataset
            return ds;
        }
    }

If anyone has any advice or suggestion I would appreciate it. Would really like to fix this issue.

CathalMF
  • 9,705
  • 6
  • 70
  • 106
gil adino
  • 67
  • 9

1 Answers1

0

Use decimal instead of double.

Decimal is more accurate and when adding up a large amount of values with decimal places.

double is not accurate for calculations on decimal places.

Recreate it using this simple code:

double a = 86.24;
double b = 86.25;
double c = b - a;

double d = 86.24;
double e = 86.25;
decimal f = (decimal)e - (decimal)d;

c.ToString();   // Incorrect result is 0.0100000000000051
f.ToString();   // Correct result is 0.01
CathalMF
  • 9,705
  • 6
  • 70
  • 106