473,386 Members | 1,908 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,386 software developers and data experts.

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
3 2632
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: DAVID SCHULMAN | last post by:
I've been trying to perform a calculation that has been running into an underflow (insufficient precision) problem in Microsoft Excel, which calculates using at most 15 significant digits. For this...
14
by: Jon Davis | last post by:
I have put my users through so much crap with this bug it is an absolute shame. I have a product that reads/writes RSS 2.0 documents, among other things. The RSS 2.0 spec mandates an en-US style...
2
by: Rod Brick | last post by:
I'm trying to print a Double in straight decimal form, not exponential. I can't seem to accomplish this. This seems like it should be simple enough. The output I'm looking for is "0.00001", not...
3
by: Dan Vogel | last post by:
I'd like to find an elegant solution to the problem of calling a certain function based on the types of two parameters. In my case, the functions compute the distance between different types of...
3
by: davesNotHere | last post by:
Greetings, experts! I'm having a bit of a problem importing some working code from last night into Visual Studio. The code compiles correctly, but when I run the program, it doesn't seem to open...
12
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
14
by: cj | last post by:
VB2003. I need a large positive integer. Which is larger int64 or double? I see int64 also apparently is known as long and will hold -9,223,372,036,854,775,808 through...
1
by: differentsri | last post by:
THIS IS AN ASP.NET 1.1 APPLICATION IAM TRYING TO UPDATE THE FIELD BUT I AM NOT ABLE TO UPDATE IT? CAN U TELL THE REASON ? IT IS GIVING THE FOLLOWING ERROR BELOW I HAVE ALSO GIVEN THE CODE OF...
5
by: schlied | last post by:
Hello, i have the following problem in our project: If I try to parse a string value with the following line double x = double.Parse("39.95238", CultureInfo.InvariantCulture); x contains...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.