DataAdapter Truncation: Feature or Bug?

G

Guest

Calling DataAdatper.Update silently truncates Decimal fields that exceed the
specified scale of the underlying database type. For example, with a scale of
2, the value 1.666 is truncated to 1.66 instead of rounded to 1.67 as you
might expect, and as it works when interacting with the database directly.

It's pernicious because it's done behind the scenes. The .Net Decimal type
doesn't have fixed scale, it adjusts to what you put in it. so you can put
these values in your DataSet with no sign of danger. It isn't until you call
Update that the problem manifests itself. The best part is that it does it
silently. I've tried but failed to come up with plausible design rationale
for this.

Has anyone else run into this? Is it by design? What's the reasoning behind
truncating intead of rounding? Shouldn't it at least throw an exception?

Yes, I know you could avoid this by rounding ahead of time, but that implies
building a whole gatekeeping layer that will have to check through the
underlying Sql types and scales and build a rounding call. That seems lame.

Thanks in advance for any insight.
 
C

Cor Ligthert [MVP]

Calling DataAdatper.Update silently truncates Decimal fields that exceed
the
specified scale of the underlying database type. For example, with a scale
of
2, the value 1.666 is truncated to 1.66 instead of rounded to 1.67 as you
might expect, and as it works when interacting with the database directly.

Who might expect, Net uses the ISO Bankers rounding standard. Although it is
not standard used in most countries.

There are now new rounding options possible in Net 2.0 what is one of the
major changes.

I hope this helps,

Cor
 
G

Guest

Cor Ligthert said:
Who might expect, Net uses the ISO Bankers rounding standard. Although it is
not standard used in most countries.

Actually more confused. Banker's rounding refers to rounding to the nearest
integer, with a special rule for .5. Which is what people
(non-floating-point-math geeks anyway ;-) normally mean when they use that
term. The .NET documentation and the ISO standard do talk about "rounding
toward zero", which is the same as truncation. Calling it rounding seems
destined to sow confusion.

The irony is that it's all unnecessary. There is no reason that I can see
that requires the rounding issue be addressed at all. The value should just
be passed on to the database and dealt with there, as you would expect.
Unless .NET views its job is to validate to prevent a problem on the DB side,
in which case it should be throwing an exception.
There are now new rounding options possible in Net 2.0 what is one of the
major changes.

[Insert sarcastic baking analogy here] ;-)
 
Top