Results 1 to 4 of 4

Thread: How to convert string to float c#

  1. #1
    Join Date
    Feb 2009
    Posts
    81

    How to convert string to float c#

    I have a string which represents a floating-point number. When i try to convert it to float number i don't get the exact floating point number. I am using visual studio NET. I'm attempting to learn some C# and I'm not too sure how to convert a String type to a float type. Does anyone know how to do this easily??

  2. #2
    Join Date
    Dec 2008
    Posts
    183

    Re: How to convert string to float c#

    The Parse method is handy because it exists for every built-in function and a few more objects besides. Use System.Convert.ToDouble(s_num) and change float to double. There are so many options in .NET, but some are almost never useful. The Parse method for the float type also has an overload which allows you to specify a System.Globalization.NumberStyle enumeration. Very handy for specifying how your string should be converted.

  3. #3
    Join Date
    Mar 2008
    Posts
    227

    Re: How to convert string to float c#

    use System.Convert.ToDouble(s_num) and change float to double. If the double value is too small to represent as a float, the result becomes positive zero or negative zero. If the double value is too large to represent as a float, the result becomes positive infinity or negative infinity.

  4. #4
    Join Date
    Apr 2008
    Posts
    193

    Re: How to convert string to float c#

    You need to cast the string to a float, or if that doesn't work the use Single.Parse.

    Code:
    private void Button1_Click(object sender, System.EventArgs e)
        {
             string s = "You have checked following Check Boxes:<BR>";
             for(int j=0;j<chkBx.Items.Count;j++)
                   {
                    if(chkBx.Items[j].Selected)
                        {
                         s += chkBx.Items[j].Text + "<BR>";
                         lblChkBx.Text = s;
                         }
                    else
                         {
                         lblChkBx.Text = "you need to Check check Boxes!";
                          }
                      }
        }
    The difference is that the second method requires that the value of txtInput.Text is a correctly formatted decimal number and will throw an exception if it isn't.

Similar Threads

  1. Convert string into int in C
    By screwball in forum Software Development
    Replies: 4
    Last Post: 22-12-2011, 08:47 PM
  2. Convert XML string into DOM
    By GreatThinker in forum Software Development
    Replies: 6
    Last Post: 22-07-2010, 09:48 AM
  3. Convert float to 2 decimal place
    By SoftWore in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 01:22 PM
  4. How to convert string into int in Java
    By Zool in forum Software Development
    Replies: 3
    Last Post: 09-11-2009, 12:41 PM
  5. How to convert string to int
    By Zavier in forum Software Development
    Replies: 3
    Last Post: 04-06-2009, 06:24 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,021,598.55008 seconds with 16 queries