Posted By: Zeus | May 20th @ 9:42 AM
page 1 of 1
Comments: 6 | Views: 495
Zeus
Zeus
Why is the caption missing??

Hi guys (and gals) ...

I have a very tricky problem around number conversion, decimal accuracy and calculations that has me scratching my head for a few days now.

In the db I have values in columns of the datatype decimal(18,2). I want to have an accuracy of 2 decimal places throughout my application and in the db it works fine.

When I am resolving the data coming from the database in a dataset, things go wrong:

float payedTotal = float.Parse(r["col_name"].ToString());

If I check the value of r["col_name"], it is 555.65, with two decimal places as expected. The float value however has the value 555.656, and rounds up to 555.70 when I display it in my application.

Am I missing something here ... or should I do the conversion and datatypes in some other way ?

stevo_
stevo_
Human after all

Firstly why aren't your using a decimal type in c#?

TommyCarlier
TommyCarlier
I want my scalps!

Stevo's right. That would change a lot. Decimal is exactly made for numbers where the accuracy is important.

Dr Herbie
Dr Herbie
Horses for courses

The only possible catch is that rounding in C# decimal type might not match the rounding used in the database if you have any rounding in your stored procedures.  By default C# decimal.Round() uses rounding to even by default while I beleive SQL Server uses 'away from zero' rounding. We always use decimal.round(value, MidpointRounding.AwayFromZero) to ensure that they match.

 Turns out Accountants seem to get quite annoyed when rounding errors put values out by a penny ...

Herbie

page 1 of 1
Comments: 6 | Views: 495
Microsoft Communities