Solved

Double.Parse C#

Posted on 2008-10-28
7
1,697 Views
Last Modified: 2013-12-17
Hi Guys,

Hopefully an easy one.


I'm parsing two strings and converting them to doubles using double.Parse .

For example,

string1 = 2.32;
string2 = 2.37.

So i use if( double.Parse(string2) > double.Parse(string1)
{
//do something
}

The problem occurs, very rarely, that it will still go into "do something" even if string2 is not greater than string1. My values usually aren't so close....by that i mean that string1 and string2 values could be a lot further apart. So i'm wondering if there is any way that the double.Parse is rounding anything off, so when the values are too close like in the example above the statement holds true.

Thanks.


0
Comment
Question by:chudmarek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 200 total points
ID: 22827610
it shouldn't be rounding unless there is an unexpected character. Double.Parse expect a "perfect" number, unexpected characters can cause an exception or an unexpected behaviour.
0
 
LVL 12

Assisted Solution

by:jandromeda
jandromeda earned 100 total points
ID: 22827708
Can you give the two values which reproduces your issue? The Parse method is not rounding the numbers.
0
 

Author Comment

by:chudmarek
ID: 22827806
Hi,

I thought that double should stay as the exact number...it is just strange.

Jandromeda, I can't give two values because the values are always changing and no two values are ever the same.

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 200 total points
ID: 22827835
where are you extracting the numbers from?
0
 
LVL 3

Expert Comment

by:trunghieubkit
ID: 22828013
as I know, there no function u want,
so u'll write it by yourself,
It's may help u,

private void button2_Click(object sender, EventArgs e)
{
	string string1 = "2.32";
	string string2 = "2.37";
 
	double d1 = double.Parse("2.321");
	double d2 = double.Parse("2.376");
 
	d1 = Truncate(d1, 2);
	d2 = Truncate(d2, 2);
 
	if (d2 > d1)
	{
		// Do
	}
}
 
private double Truncate(double val, int de)
{
	double dval = val*(Math.Pow(10,de));
 
	dval = Math.Round(dval);
 
	return dval / Math.Pow(10, de);
}

Open in new window

0
 
LVL 3

Assisted Solution

by:trunghieubkit
trunghieubkit earned 100 total points
ID: 22828022
exactly,
private void button2_Click(object sender, EventArgs e)
{
	string string1 = "2.32";
	string string2 = "2.37";
 
	double d1 = double.Parse(string1);
	double d2 = double.Parse(string2);
 
	d1 = Truncate(d1, 1);
	d2 = Truncate(d2, 1);
 
	if (d2 > d1)
	{
		// Do
	}
}
 
private double Truncate(double val, int de)
{
	double dval = val*(Math.Pow(10,de));
 
	dval = Math.Round(dval);
 
	return dval / Math.Pow(10, de);
}

Open in new window

0
 
LVL 9

Assisted Solution

by:jhshukla
jhshukla earned 100 total points
ID: 22828254
This assumes that the strings do not contain any bad characters or spaces. and that strings represent positive numbers
int decimal_pos1 = str1.IndexOf('.');
if (decimal_pos1 == -1)
    decimal_pos1 = str1.Length;
 
int decimal_pos2 = str2.IndexOf('.');
if (decimal_pos2 == -1)
    decimal_pos2 = str2.Length;
 
if (decimal_pos2 > decimal_pos1)
    /*do something*/;
else if (decimal_pos1 == decimal_pos2)
    if (str2 > str1)
        /*do something*/;

Open in new window

0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question