C# string conversion issues
-
Hello! I'm new to C# programming and I have an issue with getting a textbox string to convert to a double. I'm not sure where I went wrong in the following code, but MS Web Developer isn't liking it: dEarn = Convert.ToDouble(sEarnings); dHealth = Convert.ToDouble(txtHealth.Text); d401k = Convert.ToDouble(txt401k.Text); I'm trying this two ways: 1) Where "sEarnings" is a string equal to textbox input, and 2) Where I simply try to convert the textbox input directly. Neither way seems to work, and to add clarity, all of this is happening under a Button_Click void. Thanks!
Have you looked into using
Double.TryParse()
instead?A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
Have you looked into using
Double.TryParse()
instead?A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
Tried dEarn = Double.TryParse(sEarnings);... MS Web Developer complains "No overload for method 'TryParse' takes '1' arguments"
-
Hello! I'm new to C# programming and I have an issue with getting a textbox string to convert to a double. I'm not sure where I went wrong in the following code, but MS Web Developer isn't liking it: dEarn = Convert.ToDouble(sEarnings); dHealth = Convert.ToDouble(txtHealth.Text); d401k = Convert.ToDouble(txt401k.Text); I'm trying this two ways: 1) Where "sEarnings" is a string equal to textbox input, and 2) Where I simply try to convert the textbox input directly. Neither way seems to work, and to add clarity, all of this is happening under a Button_Click void. Thanks!
Don't use a TextBox for numeric values; use a NumericUpDown.
-
Don't use a TextBox for numeric values; use a NumericUpDown.
This is a project specifically utilizing textbox input. Many tools aren't available to me yet as I am a beginner with C#, though I do have some VB experience. So I don't necessarily know or understand all of the possible ways around an issue. Basically, think of the most common, elementary way you'd handle this problem and that is likely my answer. :-D
-
This is a project specifically utilizing textbox input. Many tools aren't available to me yet as I am a beginner with C#, though I do have some VB experience. So I don't necessarily know or understand all of the possible ways around an issue. Basically, think of the most common, elementary way you'd handle this problem and that is likely my answer. :-D
Double.TryParse and dont forget formatting and the Culture you are addressing
-
Double.TryParse and dont forget formatting and the Culture you are addressing
Thanks, Natza - but I don't understand what you mean by "culture". Here's what I'm working with, all under a Button_Click void. I've got an ASP page interacting with this back code. The user enters a number string in a textbox, I want to convert that string into a double and do a calculation. I don't want to test the input here for a boolean yes/no, I want to convert the string a use it. ------------------- protected void btnCalc_Click(object sender, System.EventArgs e) { String sName = (txtName.Text); String sEarnings = (txtEarnings.Text); String sHealth = (txtHealth.Text); String s401k = (txt401k.Text); dEarn = Convert.ToDouble(sEarnings); dHealth = Convert.ToDouble(txtHealth.Text); d401k = Convert.ToDouble(txt401k.Text); } --------------------------
-
Thanks, Natza - but I don't understand what you mean by "culture". Here's what I'm working with, all under a Button_Click void. I've got an ASP page interacting with this back code. The user enters a number string in a textbox, I want to convert that string into a double and do a calculation. I don't want to test the input here for a boolean yes/no, I want to convert the string a use it. ------------------- protected void btnCalc_Click(object sender, System.EventArgs e) { String sName = (txtName.Text); String sEarnings = (txtEarnings.Text); String sHealth = (txtHealth.Text); String s401k = (txt401k.Text); dEarn = Convert.ToDouble(sEarnings); dHealth = Convert.ToDouble(txtHealth.Text); d401k = Convert.ToDouble(txt401k.Text); } --------------------------
-
Thanks, Natza - but I don't understand what you mean by "culture". Here's what I'm working with, all under a Button_Click void. I've got an ASP page interacting with this back code. The user enters a number string in a textbox, I want to convert that string into a double and do a calculation. I don't want to test the input here for a boolean yes/no, I want to convert the string a use it. ------------------- protected void btnCalc_Click(object sender, System.EventArgs e) { String sName = (txtName.Text); String sEarnings = (txtEarnings.Text); String sHealth = (txtHealth.Text); String s401k = (txt401k.Text); dEarn = Convert.ToDouble(sEarnings); dHealth = Convert.ToDouble(txtHealth.Text); d401k = Convert.ToDouble(txt401k.Text); } --------------------------
Hi, When converting from or to strings (even calling ToString(), you should ALWAYS use the culture in which you intend the formatting for. A general rule of thumb is always use English (us or uk) for internal stuff such as application logs and exporting/importing internal data, this way even when your software or data is moved between computers with different languages it will work. Use the UI culture and number formatting in the UI layer for UI interaction. Look at this horror: 10.000 An American or english computer will read this as 10$ and a french, german and others will read this as 10000$ Fun fun fun :) I think your code should look something like this:
String sEarnings = txtEarnings.Text;
double earnings;//Use the System.Threading.Thread.CurrentThread.CurrentUICulture.NumberFormat //since the text box string data has the UI culture bool earningsParseOk = Double.TryParse( sEarnings, System.Globalization.NumberStyles.Any, System.Threading.Thread.CurrentThread.CurrentUICulture.NumberFormat, out earnings); //check whether all parsing went ok //if not alert if (earningsParseOk) { //go go go } else { //error }
-
Hello! I'm new to C# programming and I have an issue with getting a textbox string to convert to a double. I'm not sure where I went wrong in the following code, but MS Web Developer isn't liking it: dEarn = Convert.ToDouble(sEarnings); dHealth = Convert.ToDouble(txtHealth.Text); d401k = Convert.ToDouble(txt401k.Text); I'm trying this two ways: 1) Where "sEarnings" is a string equal to textbox input, and 2) Where I simply try to convert the textbox input directly. Neither way seems to work, and to add clarity, all of this is happening under a Button_Click void. Thanks!
I replied to someone else by a mistake, please see my coomment, code included
-
This is a project specifically utilizing textbox input. Many tools aren't available to me yet as I am a beginner with C#, though I do have some VB experience. So I don't necessarily know or understand all of the possible ways around an issue. Basically, think of the most common, elementary way you'd handle this problem and that is likely my answer. :-D
gtr1971 wrote:
think of the most common, elementary way you'd handle this problem and that is likely my answer
Mine too -- a NumericUpDown.