472,983 Members | 2,305 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,983 software developers and data experts.

Converting string to double

Hi, how can I parse string "? 20.000" to double ?
Nov 28 '06 #1
5 23357
Hi, how can I parse string "? 20.000" to double ?

Convert.ToDouble().

Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 28 '06 #2
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
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

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

Similar topics

4
by: rainmaker1234 | last post by:
Its very simple in VC++. In the followeing code I have declared a String, and a double than I am taking the string and converting it into Double. getch() at the end is only to pause the screen so...
1
by: Christine | last post by:
I was wondering if it is possible to convert an array that is of type string to type double so that matrix operations can be performed on it? I have a string array because I needed to split the...
4
by: diDE | last post by:
I want to convert a managed string array f.e. array<string^>^ Texts; // Elements 0: "ABC", 1: "HJO" to a TCHAR** or wchar_t** any ideas?
2
by: D. Shane Fowlkes | last post by:
Here's a good one. I've been using an Excel spreadsheet for the past couple of years to calculate a file's Estimated Download Time based off of a solid 50kbs connection (dial up). This is for a...
5
by: vivekaseeja | last post by:
Hi , Trying to convert a string value to a float value after reading the value from an XML file , but not sure what function to use. The following only works for integers Int32.Parse...
5
by: Asad | last post by:
Hi, I have retrieved values in a file. Used Double dval; String line; try{ BufferedReader in=new BufferedReader(new FileReader("File1.txt")); line = in.readLine();
5
oll3i
by: oll3i | last post by:
public class Cennik { private static Cennik instance = null; Map<String,Double> cennik = new HashMap<String,Double> (); private Cennik() { // prywatny konstruktor } public...
7
by: J | last post by:
Hi I'm just trying to convert a small program that was written in VB6 to VB.Net. I'm stumbling at the first hurdle! In VB I could do the following (ensuring that the textbox only contained...
2
by: clintonb | last post by:
Victor said: The double value that I'm trying to convert to GCSMoney (which is implemented as cents) was produced by multiplying a dollar amount by an interest rate to get interest. double...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.