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. Textbox: Convert to float and backwards

Textbox: Convert to float and backwards

Scheduled Pinned Locked Moved C#
question
8 Posts 4 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.
  • S Offline
    S Offline
    sps itsec46
    wrote on last edited by
    #1

    Hi guys! My question is certainly trivial for you... :sigh: I want to convert the text of a textbox to a float with two digits after the separator. And then the whole thing backwards: Convert the float to a string, always showing two digits after the separator. Right now I do it like that:

    float myFloat = float.Parse(TextBox1.Text); // string --> float
    TextBox1.Text = myFloat.ToString(); // float --> string

    Everything works fine except for trailing zeros (e.g. '12,30' or 123,00'). The textbox doesn't show any trailing zeros. (I use a comma as separator cause it's a german application) What can I do to show trailing zeros? Do I have to use IFormatProvider in some way? Btw... these variables represent amounts of money (€). Is it possible to set somthing like a "currency-mode" for the textbox? Many thanks in advance! :rose: mYkel

    C O 2 Replies Last reply
    0
    • S sps itsec46

      Hi guys! My question is certainly trivial for you... :sigh: I want to convert the text of a textbox to a float with two digits after the separator. And then the whole thing backwards: Convert the float to a string, always showing two digits after the separator. Right now I do it like that:

      float myFloat = float.Parse(TextBox1.Text); // string --> float
      TextBox1.Text = myFloat.ToString(); // float --> string

      Everything works fine except for trailing zeros (e.g. '12,30' or 123,00'). The textbox doesn't show any trailing zeros. (I use a comma as separator cause it's a german application) What can I do to show trailing zeros? Do I have to use IFormatProvider in some way? Btw... these variables represent amounts of money (€). Is it possible to set somthing like a "currency-mode" for the textbox? Many thanks in advance! :rose: mYkel

      C Offline
      C Offline
      Charlie Williams
      wrote on last edited by
      #2

      TextBox1.Text = myFloat.ToString("F"); Charlie if(!curlies){ return; }

      1 Reply Last reply
      0
      • S sps itsec46

        Hi guys! My question is certainly trivial for you... :sigh: I want to convert the text of a textbox to a float with two digits after the separator. And then the whole thing backwards: Convert the float to a string, always showing two digits after the separator. Right now I do it like that:

        float myFloat = float.Parse(TextBox1.Text); // string --> float
        TextBox1.Text = myFloat.ToString(); // float --> string

        Everything works fine except for trailing zeros (e.g. '12,30' or 123,00'). The textbox doesn't show any trailing zeros. (I use a comma as separator cause it's a german application) What can I do to show trailing zeros? Do I have to use IFormatProvider in some way? Btw... these variables represent amounts of money (€). Is it possible to set somthing like a "currency-mode" for the textbox? Many thanks in advance! :rose: mYkel

        O Offline
        O Offline
        OmegaSupreme
        wrote on last edited by
        #3

        TextBox1.Text = myFloat.ToString("c"); check out Chris Sells format designer to experiment with format strings : http://www.sellsbrothers.com/tools/#FormatDesigner


        The smaller the mind the greater the conceit. Aesop

        H 1 Reply Last reply
        0
        • O OmegaSupreme

          TextBox1.Text = myFloat.ToString("c"); check out Chris Sells format designer to experiment with format strings : http://www.sellsbrothers.com/tools/#FormatDesigner


          The smaller the mind the greater the conceit. Aesop

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          "C" and "c" are the format specifiers for currency, so unless you want a currency symbol in your number, don't use it. Instead, use "F" or "f" instead.

          Microsoft MVP, Visual C# My Articles

          O 1 Reply Last reply
          0
          • H Heath Stewart

            "C" and "c" are the format specifiers for currency, so unless you want a currency symbol in your number, don't use it. Instead, use "F" or "f" instead.

            Microsoft MVP, Visual C# My Articles

            O Offline
            O Offline
            OmegaSupreme
            wrote on last edited by
            #5

            Good point, he did say 'Is it possible to set somthing like a "currency-mode" for the textbox?' though so I though thats what he wanted. Cheers.


            The smaller the mind the greater the conceit. Aesop

            H 1 Reply Last reply
            0
            • O OmegaSupreme

              Good point, he did say 'Is it possible to set somthing like a "currency-mode" for the textbox?' though so I though thats what he wanted. Cheers.


              The smaller the mind the greater the conceit. Aesop

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              You're absolutely right, sorry! A lot of sentences in there and I guess I missed the last one. :-O In any case - to the original poster if you're following along - you should look at the CultureInfo class and its related properties (IFormatProvider implementations). For most ToString overloads (not the override from Object or another base class), the IFormatProvider (like the NumberFormatInfo) is used for the Thread.CurrentUICulture, so you don't really need to specify one unless you want to format for a different culture (just for that instance, otherwise you should set Thread.CurrentUICulture and re-initialize your controls) or pass your own IFormatProvider. Typically, such a ToString method (or methods like String.Format) will specify if they use the current CultureInfo for the format specifier.

              Microsoft MVP, Visual C# My Articles

              S 1 Reply Last reply
              0
              • H Heath Stewart

                You're absolutely right, sorry! A lot of sentences in there and I guess I missed the last one. :-O In any case - to the original poster if you're following along - you should look at the CultureInfo class and its related properties (IFormatProvider implementations). For most ToString overloads (not the override from Object or another base class), the IFormatProvider (like the NumberFormatInfo) is used for the Thread.CurrentUICulture, so you don't really need to specify one unless you want to format for a different culture (just for that instance, otherwise you should set Thread.CurrentUICulture and re-initialize your controls) or pass your own IFormatProvider. Typically, such a ToString method (or methods like String.Format) will specify if they use the current CultureInfo for the format specifier.

                Microsoft MVP, Visual C# My Articles

                S Offline
                S Offline
                sps itsec46
                wrote on last edited by
                #7

                Thanks so much for your numerous and quick answers! I knew there is a simple solution... ;) The myFloat.ToString("F") does all I need. There's no difference between "f" and "F", right? Regards, mYkel

                H 1 Reply Last reply
                0
                • S sps itsec46

                  Thanks so much for your numerous and quick answers! I knew there is a simple solution... ;) The myFloat.ToString("F") does all I need. There's no difference between "f" and "F", right? Regards, mYkel

                  H Offline
                  H Offline
                  Heath Stewart
                  wrote on last edited by
                  #8

                  Mykel wrote: There's no difference between "f" and "F", right? Nope. See the documentation for NumberFormatInfo in the .NET Framework SDK for more information.

                  Microsoft MVP, Visual C# My Articles

                  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