Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

Specify precision of decimal?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Anodes - 01 Apr 2008 16:19 GMT
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.
Jon Skeet [C# MVP] - 01 Apr 2008 16:24 GMT
> I have a decimal variable. I'd like it to store a number rounded to
> two decimal places.
[quoted text clipped - 14 lines]
> 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?

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Anodes - 01 Apr 2008 17:56 GMT
> > I have a decimal variable. I'd like it to store a number rounded to
> > two decimal places.
[quoted text clipped - 22 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

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);
Jon Skeet [C# MVP] - 01 Apr 2008 18:27 GMT
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Gregg Walker - 01 Apr 2008 18:43 GMT
> 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.
--
Gregg Walker
Ignacio Machin ( .NET/ C# MVP ) - 01 Apr 2008 18:40 GMT
> I have a decimal variable. I'd like it to store a number rounded to
> two decimal places.
[quoted text clipped - 16 lines]
>
> 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.
Gregg Walker - 01 Apr 2008 19:00 GMT
> 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.
--
Gregg Walker
Ignacio Machin ( .NET/ C# MVP ) - 02 Apr 2008 21:28 GMT
> > 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
[quoted text clipped - 6 lines]
> 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 !!!!
Arne Vajhøj - 14 Apr 2008 00:05 GMT
>>> 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
[quoted text clipped - 11 lines]
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.