By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,652 Members | 1,694 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,652 IT Pros & Developers. It's quick & easy.

Converting string to double

P: n/a
Hi, how can I parse string "? 20.000" to double ?
Nov 28 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi, how can I parse string "? 20.000" to double ?

Convert.ToDouble().

Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 28 '06 #2

P: n/a
SMichal wrote:
Hi, how can I parse string "? 20.000" to double ?

Well, you obviously need to remove the '?' character and space before
it can be parsed to any numerical format. So I would use the Split()
function to remove the '?' character and the Trim() function to remove
the space:

string strNumber = "? 20.000";
strNumber = strNumber.Split('?');
strNumber.Trim();

Then use the Convert.ToDouble() function to convert the string to a
double value:

double myVal = Convert.ToDouble(strNumber);

If the value you are converting should be represented as currency or
requires a certain degree of precision you should use the
String.Format() function after converting your value. I hope that helps.

Nov 28 '06 #3

P: n/a
This would rely on the garbage text that proceeds the number always
being in the same format.

I think it would be better to convert the string to a character array
and examine each character for numeric validity before adding it to the
formatted string and parsing this as a double.

I've had a go but as people familliar to this forum may know, i'm a
complete beginner. My code doesn't work as it stands, perhaps someone
could offer a modification?

private void button1_Click(object sender, EventArgs e)
{
string somestring = "? 20.00";
char[] stringarray = somestring.ToCharArray();
string formattedstring = "0";
foreach (char c in stringarray)
{
if (((int)c >= 0 && (int)c <= 9) || c == '.')
{
formattedstring += c;
}
else
{
break;
}
}
double myDouble = double.Parse(formattedstring);
MessageBox.Show(myDouble.ToString());
}

Gary.;

Nov 28 '06 #4

P: n/a

Hey SMichal,

I cannot speak more glowingly about regular expressions ... try this
out:

//
// Setup our regex
//
Regex r = new Regex(@"(\d+.\d+)");
//
// Get the input
//
string input = "? 20.0000";
//
// Capture the value
//
string capturedValue = r.Match(input).Value;
//
// convert
//
double result = Double.Parse(capturedValue);

It will extract the doubles from any given string

On Tue, 28 Nov 2006 15:57:25 +0100, "SMichal"
<mi************@trygon-softwareberatung.dewrote:
>Hi, how can I parse string "? 20.000" to double ?
--

Bits.Bytes.
http://bytes.thinkersroom.com
Nov 28 '06 #5

P: n/a
On 28 Nov 2006 07:51:06 -0800, ga********@myway.com wrote:
>This would rely on the garbage text that proceeds the number always
being in the same format.

I think it would be better to convert the string to a character array
and examine each character for numeric validity before adding it to the
formatted string and parsing this as a double.

I've had a go but as people familliar to this forum may know, i'm a
complete beginner. My code doesn't work as it stands, perhaps someone
could offer a modification?

private void button1_Click(object sender, EventArgs e)
{
string somestring = "? 20.00";
char[] stringarray = somestring.ToCharArray();
string formattedstring = "0";
foreach (char c in stringarray)
{
if (((int)c >= 0 && (int)c <= 9) || c == '.')
{
formattedstring += c;
}
else
{
break;
}
}
double myDouble = double.Parse(formattedstring);
MessageBox.Show(myDouble.ToString());
}

Gary.;
First problem: When you enter your foreach loop, the first character
is '?', which activates the break; thus terminating the loop having
only processed one character. Remove the else part entirely as you
want to process all the characters in stringarray.

When you try this, you will find that it still does not work as you
expected.

Second problem: Your if statement starts:

if (((int)c >= 0 && (int)c <= 9) || c == '.')

When c = '2', (int)c is not 2, but the UTF encoding for 2. Change the
if to read:

if ((c >= '0' && c <= '9') || c == '.')

You did this correctly with the decimal point.

Your code now works after a fashion, though "20.02" would be a better
test number than 20.00. Try it to see why.

You ought to try running your code in the debugger. I just put one
breakpoint at the strat of the foreach loop, which found both your
problems easily.

Other points you may need to consider.

1 C# strings are immutable, you cannot change them. All you can do is
replace the old copy of the string with the new version. Better in
general to use a stringbuilder rather than a string in cases such as
this, where you do not know how long the final string is going to be.

2 Some countries use other characters such as ',' instead of '.' as a
decimal separator. Have a look at
System.Globalization.NumberFormatInfo.NumberDecima lSeparator.

3 You have no protection against a string like:
"? 20.02 ? 20.02 ? 20.03"
Try it and see what happens.

As a learning exercise you might also want to try writing a finite
state machine to parse real numbers out of a text string. It makes an
interesting little exercise.

rossum

Nov 28 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.