Specify precision of decimal?

A

Anodes

I have a decimal variable. I'd like it to store a number rounded to
two decimal places.

I've declared and loaded it like this:

CODE
private Decimal FileSize_MB_DataFile = 0.0M;
this.FileSize_MB_InitialFile = Convert.ToDecimal(fileInfo.Length) /
1048576; //1024 x 1024

I want the actual value of the variable to be, for example, .75 or
15.28 or whatever (I don't want to convert it to a string and format
it unless that's strictly necessary). When I look at the value of the
variable in the debugger it goes out to like 15 decimal places. I'm
sure this is quite simple.

I realize I could keep the precision but these values are going into a
database and I don't want/need the extra digits.

Thanks.
 
J

Jon Skeet [C# MVP]

Anodes said:
I have a decimal variable. I'd like it to store a number rounded to
two decimal places.

I've declared and loaded it like this:

CODE
private Decimal FileSize_MB_DataFile = 0.0M;
this.FileSize_MB_InitialFile = Convert.ToDecimal(fileInfo.Length) /
1048576; //1024 x 1024

I want the actual value of the variable to be, for example, .75 or
15.28 or whatever (I don't want to convert it to a string and format
it unless that's strictly necessary). When I look at the value of the
variable in the debugger it goes out to like 15 decimal places. I'm
sure this is quite simple.

I realize I could keep the precision but these values are going into a
database and I don't want/need the extra digits.

No, the decimal type always has the same level of precision (28 or 29
significant digits, IIRC). Can't you specify the precision in the
database instead?
 
A

Anodes

No, the decimal type always has the same level of precision (28 or 29
significant digits, IIRC). Can't you specify the precision in the
database instead?

Sure, but I'd like to specify it in code. I'm going with this:
this.FileSize_MB_InitialFile =
Math.Round(Convert.ToDecimal(fileInfo.Length) /
1048576), 2);
 
J

Jon Skeet [C# MVP]

Anodes said:
Sure, but I'd like to specify it in code. I'm going with this:
this.FileSize_MB_InitialFile =
Math.Round(Convert.ToDecimal(fileInfo.Length) /
1048576), 2);

You'll need to accept that the decimal type has no concept of having a
customisable precision. You could write your own type if you want, but
that's likely to be a lot of work for little benefit.
 
I

Ignacio Machin ( .NET/ C# MVP )

I have a decimal variable. I'd like it to store a number rounded to
two decimal places.

I've declared and loaded it like this:

CODE
private Decimal FileSize_MB_DataFile = 0.0M;
this.FileSize_MB_InitialFile = Convert.ToDecimal(fileInfo.Length) /
1048576; //1024 x 1024

I want the actual value of the variable to be, for example, .75 or
15.28 or whatever (I don't want to convert it to a string and format
it unless that's strictly necessary). When I look at the value of the
variable in the debugger it goes out to like 15 decimal places. I'm
sure this is quite simple.

I realize I could keep the precision but these values are going into a
database and I don't want/need the extra digits.

Thanks.

Hi,

I think it's a bad idea doing that. you can display the value with two
decimal places but you should keep as many as it allows. Otherwise you
can get into rounding problems when you operate in these values.
 
G

Gregg Walker

Sure, but I'd like to specify it in code. I'm going with this:
this.FileSize_MB_InitialFile =
Math.Round(Convert.ToDecimal(fileInfo.Length) /
1048576), 2);

Caveat emptor with Math.Round ...

Be sure you understand how MidpointRounding is implemented in the default
overloads. I ended up with a "wrong" rounding for many calculations because
the default MidpointRounding (ToEven) uses "bankers rounding" which is not
used in our industry. We use MidpointRounding.AwayFromZero which is what I
learned in high school and have seen used every where I have worked.
 
G

Gregg Walker

I think it's a bad idea doing that. you can display the value with two
decimal places but you should keep as many as it allows. Otherwise you
can get into rounding problems when you operate in these values.

Ignacio -- I have to respectfully disagree with what you stated.

IMHO rounding needs to be applied precisely when business logic dictates
that it be applied. Not before and not after. Applying rounding at the
wrong time (calculation) is what leads to the future rounding problems as
you suggested.

If Anodes business logic requires that he store a value with 2 decimal
places in the database then rounding before the database operation takes
place is the appropriate time. If not then not.
 
I

Ignacio Machin ( .NET/ C# MVP )

Ignacio -- I have to respectfully disagree with what you stated.

IMHO rounding needs to be applied precisely when business logic dictates
that it be applied.  Not before and not after.  Applying rounding at the
wrong time (calculation) is what leads to the future rounding problems as
you suggested.

I think I suggested exactly the opposite, do not do rounding in the
calculations (or in the storage of the values) only when you need to
display the value you should roundit.
Agree that the above is the general case, it might be cases where it
does not apply, IMHO those are the few.
If Anodes business logic requires that he store a value with 2 decimal
places in the database then rounding before the database operation takes
place is the appropriate time.  If not then not.

Of course, it for some reason the logic requires that, then by all
mean do it like that.
Most of the cases though that is not the case. and you can get nasty
side effects from it.
For example, if you are adding a large quantity of values you can get
a big difference.
Let's say that you have 1kk rows which "real" value is 10.0001, if
you round it to 10.00 when you sum all the rows you have a difference
of 100 !!!!
 
A

Arne Vajhøj

Ignacio said:
I think I suggested exactly the opposite, do not do rounding in the
calculations (or in the storage of the values) only when you need to
display the value you should roundit.
Agree that the above is the general case, it might be cases where it
does not apply, IMHO those are the few.

Decimal is mostly used for money.

My experience is the same as Gregg's: the business rules determine
when rounding should happen and it is not necessarily latest moment.
Rounding twice at different places in the calculations are also
sometimes seen.

For float and decimal I completely agree with you - round when
outputting. But accountants think differently.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top