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. I need to convert a string to a float....

I need to convert a string to a float....

Scheduled Pinned Locked Moved C#
visual-studiodata-structuresquestion
8 Posts 5 Posters 18 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
    glennPattonWork3
    wrote on last edited by
    #1

    Hi All,... I am reading into a rich text box a small number (0.385 to 0.457) which is read into a Rich Text Box as a string and my though was to use Text.ConvertTo(float) in the following way

    Value_Extract = ((Convert.ToDecimal(rtbIncoming.Text));

    Value_Extract is a float... This does not work compiles and Bang falls over when run, had look on Stack Overflow they seem to do in a similar way. Is this the correct way?? As VS helpfully tells me I need to cast a float as a decimal, decimal.tryparse??

    R Richard DeemingR B 3 Replies Last reply
    0
    • G glennPattonWork3

      Hi All,... I am reading into a rich text box a small number (0.385 to 0.457) which is read into a Rich Text Box as a string and my though was to use Text.ConvertTo(float) in the following way

      Value_Extract = ((Convert.ToDecimal(rtbIncoming.Text));

      Value_Extract is a float... This does not work compiles and Bang falls over when run, had look on Stack Overflow they seem to do in a similar way. Is this the correct way?? As VS helpfully tells me I need to cast a float as a decimal, decimal.tryparse??

      R Offline
      R Offline
      Ron Nicholson
      wrote on last edited by
      #2

      I typically use float.TryParse(string, out float result); This returns true if successful and false if not. HTH

      Jack of all trades, master of none, though often times better than master of one.

      G 1 Reply Last reply
      0
      • R Ron Nicholson

        I typically use float.TryParse(string, out float result); This returns true if successful and false if not. HTH

        Jack of all trades, master of none, though often times better than master of one.

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

        Hi, Thanks for that float.TryParse(), will I be able to get out as a floating point?..

        OriginalGriffO 1 Reply Last reply
        0
        • G glennPattonWork3

          Hi All,... I am reading into a rich text box a small number (0.385 to 0.457) which is read into a Rich Text Box as a string and my though was to use Text.ConvertTo(float) in the following way

          Value_Extract = ((Convert.ToDecimal(rtbIncoming.Text));

          Value_Extract is a float... This does not work compiles and Bang falls over when run, had look on Stack Overflow they seem to do in a similar way. Is this the correct way?? As VS helpfully tells me I need to cast a float as a decimal, decimal.tryparse??

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Are you sure it compiles? String doesn't have a ConvertTo method, and you can't pass the Decimal type as an argument like that. Assuming the text is a valid number, then Convert.ToSingle(rtdData.Text) should give you a float back. Or Convert.ToDecimal(rtdData.Text) would give you a decimal, which you would then have to cast to store in a float variable. But these methods will throw an exception if the text is not a valid number. It would be better to use float.TryParse / decimal.TryParse so that you can notify the user if the value can't be parsed.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          G 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            Are you sure it compiles? String doesn't have a ConvertTo method, and you can't pass the Decimal type as an argument like that. Assuming the text is a valid number, then Convert.ToSingle(rtdData.Text) should give you a float back. Or Convert.ToDecimal(rtdData.Text) would give you a decimal, which you would then have to cast to store in a float variable. But these methods will throw an exception if the text is not a valid number. It would be better to use float.TryParse / decimal.TryParse so that you can notify the user if the value can't be parsed.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            G Offline
            G Offline
            glennPattonWork3
            wrote on last edited by
            #5

            Okay, I will give that a go... It hasn't crashed yet!!

            1 Reply Last reply
            0
            • G glennPattonWork3

              Hi, Thanks for that float.TryParse(), will I be able to get out as a floating point?..

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              Yes:

              if (float.TryParse(myTextBox.Text, out float result))
              {
              Console.WriteLine(result);
              }

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • G glennPattonWork3

                Hi All,... I am reading into a rich text box a small number (0.385 to 0.457) which is read into a Rich Text Box as a string and my though was to use Text.ConvertTo(float) in the following way

                Value_Extract = ((Convert.ToDecimal(rtbIncoming.Text));

                Value_Extract is a float... This does not work compiles and Bang falls over when run, had look on Stack Overflow they seem to do in a similar way. Is this the correct way?? As VS helpfully tells me I need to cast a float as a decimal, decimal.tryparse??

                B Offline
                B Offline
                Bosse62
                wrote on last edited by
                #7

                Is there a problem with the decimal sign; period or comma?

                G 1 Reply Last reply
                0
                • B Bosse62

                  Is there a problem with the decimal sign; period or comma?

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

                  Old question now solved!

                  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