467,911 Members | 1,370 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,911 developers. It's quick & easy.

System.Double.Parse not accurate?

I've got a section of code in my app that keeps track of how much time an
employee has accrued, which equals out to 1.33 days of PTO per month. I'm
trying to store the time accrued so far in an XML file. I have no problems
writing to and reading from the XML file and all my values are correct. The
problem I'm having is my calculation never amounts to 1.33, it goes from
1.32999999999997
to
1.3743333333333.
on the next run.

My calculation is
int TimePerDay = 1.33 /
DateTime.DaysInMonth(DateTime.Parse(DateTime.Now.T oShortDateString()).Year,
DateTime.Parse(DateTime.Now.ToShortDateString()).M onth);

I save the difference of that calculation in the XML file, and then retrieve
it again when the calculation is necessary. Here is the code:
int TimeCount =
System.Double.Parse(xmlDoc.ChildNodes.Item(1).Chil dNodes.Item(1).InnerText);
//the first time I retrieve this value, it will be initially set to
0.0443333333333333
//the first time this code runs
TimeCount += 1.33 /
DateTime.DaysInMonth(DateTime.Parse(DateTime.Now.T oShortDateString()).Year,
DateTime.Parse(DateTime.Now.ToShortDateString()).M onth);
xmlDoc.Save(Path);

Like I said the problem is that calculation never amounts to 1.33 - I have
ran it many times in a for loop without ever converting it to a string value.
The for loop eventually calculates to 1.33, and it stops at 1.33 and
displays that fact in a MessageBox. I think I may be having a problem with
the System.Double.Parse parser . . . anybody got any idea's? :)
Nov 16 '05 #1
  • viewed: 2406
Share:
3 Replies
slylos wrote:
I've got a section of code in my app that keeps track of how much
time an employee has accrued, which equals out to 1.33 days of PTO
per month. I'm trying to store the time accrued so far in an XML
file. I have no problems writing to and reading from the XML file
and all my values are correct. The problem I'm having is my
calculation never amounts to 1.33, it goes from
1.32999999999997
to
1.3743333333333.
on the next run.


See
http://www.yoda.arachsys.com/csharp/floatingpoint.html

for the reasons floating point calculations come out "wrong"

Hans Kesting
Nov 16 '05 #2
The article you provided was very useful. Thank you very much for that
information. Now let me get back to my code and see what I come up with!

"Hans Kesting" wrote:
slylos wrote:
I've got a section of code in my app that keeps track of how much
time an employee has accrued, which equals out to 1.33 days of PTO
per month. I'm trying to store the time accrued so far in an XML
file. I have no problems writing to and reading from the XML file
and all my values are correct. The problem I'm having is my
calculation never amounts to 1.33, it goes from
1.32999999999997
to
1.3743333333333.
on the next run.


See
http://www.yoda.arachsys.com/csharp/floatingpoint.html

for the reasons floating point calculations come out "wrong"

Hans Kesting

Nov 16 '05 #3
I found that if I take the
NewTime variable, which I read the latest floating point value into from the
XML file, if I modulo it by 1.33, I eventually get 1.33 exact when NewTime
actually sums to 1.33000000000012. I can then check for the modulo value to
get the desired result. Thanks for your help all!!

"Hans Kesting" wrote:

See
http://www.yoda.arachsys.com/csharp/floatingpoint.html

for the reasons floating point calculations come out "wrong"

Hans Kesting

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by DAVID SCHULMAN | last post: by
3 posts views Thread by Dan Vogel | last post: by
3 posts views Thread by davesNotHere | last post: by
12 posts views Thread by Frederik Vanderhaeghe | last post: by
14 posts views Thread by cj | last post: by
5 posts views Thread by schlied | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.