Problem: DataRow alters stored System.Decimal values.

  • Thread starter Thread starter Carl G
  • Start date Start date
C

Carl G

I am storing a 0.000 a System.Decimal in a DataRow.
On retrieval the value is only 0 without the three decimal places.

It looks like the Get property returns System.Decimal.Zero, but
why????
I can't figure out why the design is so that the DataRow "alters" the
value entered.

In my application a decimal column in a row of a specific table has a
fix number of decimal places according to certain premises. The
premises are quite tedious to calculate so it would be VERY nice if I
could figure out the number of decimal places by looking at the value
stored. However since 0 (zero) can't be stored with a fix amount of
decimal places, I have to store the number of decimal places for each
column in a secondary information source. This secondary information
source gets really huge. For example: one table out of 255 has about
7000 rows with three decimal columns per row.


Considering the following code

DataRow myRow = myTable.NewRow();

Decimal myDec = new Decimal( 0, 0, 0, false, 3 ); // 0.000
Console.WriteLine(myDecimal + " " + NumberOfDec(myDec));

myRow["decimal"] = myDec;
Console.WriteLine(myRow["decimal"].ToString() + " " +
NumberOfDec((Decimal)myRow["decimal"]));

Where NumberOfDec is

public static int NumberOfDec(System.Decimal decVal)
{
int[] bits = System.Decimal.GetBits(decVal);
int retVal= Convert.ToInt32(System.BitConverter.GetBytes(bits[3])[2]);
return retVal;
}

The output of this code is
0 3
0 0

In other words myDec is 0 (zero) but with 3 decimals. But when adding
it to myRow it loses the information about 3 decimals.

When looking at myDec and myRow["decimal"]) in the QuickWatch myDec's
flag property is 196608 but myRow["decimal"]) flag property is 0.

Any suggestions/explanations, is it a bug??

/Carl
 
why do you have to store the decimal places when they are 0? Just to display
the in the DataRow? Then why don't you just format the output to show 3
decimal places?

Maybe I don't understand your question but it seems like you are storing
data unnecessarily.
 
why do you have to store the decimal places when they are 0? Just to display
the in the DataRow? Then why don't you just format the output to show 3
decimal places?

I don't want to store the decimal places. I want to use the fact that
System.Decimal holds the information for me. When the user changes the
field that was 0 (to for instance 0.1234) I want to be able to easily
understand how many decimal places it should have by looking at the
prvious value. It works fine for instance if the value is 1.23 and the
user enters 1.5644, I then now that I should round it to two decimals
1.56

In my application, the number of decimal places can vary for the same
column between different data rows depending on the value of other
columns. Calculating this takes time. So by using the fact that
System.Decimal stores the number of decimal places, I could have speed
things up.
Maybe I don't understand your question but it seems like you are storing
data unnecessarily.

Yes it is unnecessarily. But I do not have a choice since I can't
store 0 in the datarow as 0.000. Storing 3.000 works fine, but not
0.000.
 
Back
Top