Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# string conversion issues

C# string conversion issues

Scheduled Pinned Locked Moved C#
csharphelp
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gtr1971
    wrote on last edited by
    #1

    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!

    D P N 3 Replies Last reply
    0
    • G gtr1971

      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!

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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...

      G 1 Reply Last reply
      0
      • D Dave Kreskowiak

        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...

        G Offline
        G Offline
        gtr1971
        wrote on last edited by
        #3

        Tried dEarn = Double.TryParse(sEarnings);... MS Web Developer complains "No overload for method 'TryParse' takes '1' arguments"

        L 1 Reply Last reply
        0
        • G gtr1971

          Tried dEarn = Double.TryParse(sEarnings);... MS Web Developer complains "No overload for method 'TryParse' takes '1' arguments"

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Try reading the documentation[^].

          1 Reply Last reply
          0
          • G gtr1971

            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!

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Don't use a TextBox for numeric values; use a NumericUpDown.

            G 1 Reply Last reply
            0
            • P PIEBALDconsult

              Don't use a TextBox for numeric values; use a NumericUpDown.

              G Offline
              G Offline
              gtr1971
              wrote on last edited by
              #6

              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

              N P 2 Replies Last reply
              0
              • G gtr1971

                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

                N Offline
                N Offline
                Natza Mitzi
                wrote on last edited by
                #7

                Double.TryParse and dont forget formatting and the Culture you are addressing

                Natza Mitzi Analysis Studio Statistical Analysis Software

                G 1 Reply Last reply
                0
                • N Natza Mitzi

                  Double.TryParse and dont forget formatting and the Culture you are addressing

                  Natza Mitzi Analysis Studio Statistical Analysis Software

                  G Offline
                  G Offline
                  gtr1971
                  wrote on last edited by
                  #8

                  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); } --------------------------

                  L N 2 Replies Last reply
                  0
                  • G gtr1971

                    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); } --------------------------

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    I gave you the answer; see my message above.

                    1 Reply Last reply
                    0
                    • G gtr1971

                      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); } --------------------------

                      N Offline
                      N Offline
                      Natza Mitzi
                      wrote on last edited by
                      #10

                      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
                              }
                      

                      Natza Mitzi Analysis Studio Statistical Analysis Software

                      1 Reply Last reply
                      0
                      • G gtr1971

                        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!

                        N Offline
                        N Offline
                        Natza Mitzi
                        wrote on last edited by
                        #11

                        I replied to someone else by a mistake, please see my coomment, code included

                        Natza Mitzi Analysis Studio Statistical Analysis Software

                        1 Reply Last reply
                        0
                        • G gtr1971

                          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

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #12

                          gtr1971 wrote:

                          think of the most common, elementary way you'd handle this problem and that is likely my answer

                          Mine too -- a NumericUpDown.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups