473,471 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Wrong result with double.Parse()

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 39.9523799999.... instead of the correct one.
What could be the reason for that?

Thanks in advance

Chris
Jun 27 '08 #1
5 4575
sc*****@gmail.com wrote:
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 39.9523799999.... instead of the correct one.
What could be the reason for that?

Thanks in advance

Chris
The reason would be that floating point types are not accurate, and
store an approximation of the value. You'll find that you will
frequently notice two variations of this, either slightly lower
(.99999999) or slightly higher (.0000000001).

Try using Decimal, or just format the value for output to round to a
specific number of digits.

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Jun 27 '08 #2
sc*****@gmail.com explained on 20-6-2008 :
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 39.9523799999.... instead of the correct one.
What could be the reason for that?

Thanks in advance

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

Hans Kesting
Jun 27 '08 #3
Double cannto guarantee to exactly represent every value, due to the
nature of floating-point arithmetic.

It sounds like you actually want a decimal; try:

decimal x = decimal.Parse("39.95238", CultureInfo.InvariantCulture);

Marc
Jun 27 '08 #4
On Jun 20, 2:31 pm, schl...@gmail.com wrote:
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 39.9523799999.... instead of the correct one.
What could be the reason for that?

Thanks in advance

Chris
http://www.yoda.arachsys.com/csharp/floatingpoint.html has good
information....

however, I did not face any such error using VS2008

-Cnu
Jun 27 '08 #5
sc*****@gmail.com wrote:
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 39.9523799999.... instead of the correct one.
Looks right to me... 7.9 repeating is equal to 8. Even if not repeating,
the total error is what, less than one trillionth of a percent?
What could be the reason for that?

Thanks in advance

Chris

Jul 18 '08 #6

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

Similar topics

5
by: Jepsensen | last post by:
Dear Everybody. I got a problem with my cpp code. I'm trying to calculate a very simple Discrete Cosine Transform (DCT), but my c++ code seams to calculate a wrong result. I have just started...
4
by: PengYu.UT | last post by:
Hi, It seems that the line with "//no error" and "//error" are the same. However, the second one give an error. I'm using g++-3.3. The error message was: g++-3.3 -g -c -o main.o main.cc...
2
by: atv | last post by:
Hi, is there something wrong with this code? It seems that it crashes at the else { clause, when allocating a second record. Excuse for the indentation, the g_warnings are from gtk. Also, i'm...
7
by: cw bebop | last post by:
HTML code: <form id="Form1" method="post" runat="server"> <asp:TextBox id="TextBox1" style="Z-INDEX: 105; LEFT: 120px; POSITION: absolute; TOP: 64px" runat="server" Width="144px"></asp:TextBox>...
8
by: - | last post by:
Hi to All, To reproduce: The expression: object result = flag ? (long) 0 : (double) 0; always evaluated as a double... see dissassembly to ensure the bad compiled code.
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
38
by: DaVinci | last post by:
#include<stdio.h> #include<stdlib.h> #include<string.h> #define SECONDS_PER_YEAR (60*60*24*365)UL int main() { unsigned long int u_i = SECONDS_PER_YEAR; printf("%lu\n",u_i);
30
by: Bill Reid | last post by:
#define MAX_VALUES 64 typedef struct { unsigned value_1; double value_2; double value_3; double value_4; } VALUES; typedef struct {
3
by: Army1987 | last post by:
I can see no problem with the loop below, and gcc compiles it without complaining no matter how many warnings I enable, but splint has a parse error on line 103 (marked below), column 22. Is it...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.