i tried the above code with Decimal datatype and it works fine....is it possible for you to chnage the datatype from double to decimal
as for why it is happening...i am not able to figure it out...still looking...
Main Topics
Browse All TopicsHi,
we have some simple C# code that accesses a data table containing weights for a portfolio. Each weight is expressed in decimal with 4 digits, with the restriction that they must add up to 100(don't need to be positive, although in this case and most practile applications they are). For example, a set of weights for a portfolio containing 5 assets is the following:
0.1469
0.1594
0.1525
0.3887
0.1525
=====
1.0000
The weights are input via an Infragistics table control, linked to a .NET data table, the code below goes over each row (portfolio asset), extracts the weight and computes a running total:
// dtPoints is System.Data.DataTable object
double dblSum = 0.0;
foreach(DataRow dRow in dtPoints.Rows)
{
if ( dRow[PortfolioPointInfo.Co
double dblWeight = Convert.ToDouble( dRow[PortfolioPointInfo.Co
dblSum = dblSum + dblWeight;
}
The first 3 iterations are ok, but on the 4-th one the running total gets rounded:
Iteration #1: Weight = 0.1469, Running Total = 0.1469
Iteration #2: Weight = 0.1594, Running Total = 0.3063
Iteration #3: Weight = 0.1525, Running Total = 0.4588,
Iteration #4: Weight = 0.3887, Running Total = 0.84749999999999992
Why the rounding? Cannot understand why it is happening. All numbers use 4 decimals, so I expect the total to have 4 decimals as well.
Any suggestion to help understand this will be much appreciated!
Best,
Stefano
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Yes, I believe it is possible to change it by using Convert class.
As for why it is happening... Well, its to do with the way the computer represents real numbers in binary. You'll find more information on:
http://en.wikipedia.org/wi
But basicly it's saying that, floating-point numbers cannot represent real numbers exactly but a very closely approximates.
HTH,
Arca
Business Accounts
Answer for Membership
by: ArcaArtemPosted on 2006-10-16 at 23:43:28ID: 17745210
Hi,
Floating point variables cannot hold an exact value but an approximation. For example double d = 1.2 will not cause d to have 1.2 but 1.19999999...
It's best to use "decimal" and I believe it fits perfectly to your requirement.
HTH,
Arca