473,388 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Specify precision of decimal?

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.
Apr 1 '08 #1
4 4731
On Apr 1, 11:19*am, Anodes <jjs...@goowy.comwrote:
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.

Apr 1 '08 #2
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
Apr 1 '08 #3
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
Apr 1 '08 #4
Ignacio Machin ( .NET/ C# MVP ) wrote:
On Apr 1, 2:00 pm, "Gregg Walker" <xy...@newsgroup.nospamwrote:
>>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.

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
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Ladvánszky Károly | last post by:
Entering 3.4 in Python yields 3.3999999999999999. I know it is due to the fact that 3.4 can not be precisely expressed by the powers of 2. Can the float handling rules of the underlying layers be...
4
by: Roger Leigh | last post by:
Hello, I'm writing a fixed-precision floating point class, based on the ideas in the example fixed_pt class in the "Practical C++ Programming" book by Steve Oualline (O' Reilly). This uses a...
2
by: Brian van den Broek | last post by:
Hi all, I guess it is more of a maths question than a programming one, but it involves use of the decimal module, so here goes: As a self-directed learning exercise I've been working on a...
1
by: Tariq | last post by:
I've a SQL view performing a number of very precise calculations ie. 495/(1.112 - (.00043499*((Col1 + Col2 + Col3))) + ( .00000055 * ((Col1 + Col2 + Col3)) * ((Col1 + Col2 + Col3))) - (.00028826...
3
by: James Foreman | last post by:
I'm using DB2 UDB 8.1.2 on Suse linux. Try this: CREATE TABLE testsums (cost_of_sale decimal(7,2), price decimal(9, 2)); INSERT INTO testsums VALUES (10000, 30000); SELECT cost_of_sale/price...
5
by: Robin Tucker | last post by:
My database has a column for numeric data items. How can I use this number in the "format" command, such that for, say precision 2, I get numbers like 2011.01 or 2387.00 and for 4 I would get...
15
by: Donkey | last post by:
Hi, The precision of built-in date type of C is very low. Even using long double float type or double float type, we can only use 12 or 16 digits after the decimal point. What can we do if we want...
2
by: Sam | last post by:
Hi, I would like to be able to read the scale & precision of a decimal type contained in a sql server table from vb.net. I would like to set some properties (like width,format) of a gridview's...
3
by: Vamsi | last post by:
Hi, I have a column defined as decimal(4,3). I get the following error when I try to insert "12.50000000000000" CLI0111E Numeric value out of range. SQLSTATE=22003 sqlstate = 22003 I even...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.