Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 14-08-2009
Member
 
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??
Reply With Quote
  #2  
Old 14-08-2009
Member
 
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.
Reply With Quote
  #3  
Old 14-08-2009
Member
 
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.
Reply With Quote
  #4  
Old 14-08-2009
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to convert string to float c#"
Thread Thread Starter Forum Replies Last Post
Convert string into int in C screwball Software Development 4 22-12-2011 08:47 PM
Convert XML string into DOM GreatThinker Software Development 6 22-07-2010 09:48 AM
Convert float to 2 decimal place SoftWore Software Development 3 26-11-2009 01:22 PM
How to convert string into int in Java Zool Software Development 3 09-11-2009 12:41 PM
How to convert string to int Zavier Software Development 3 04-06-2009 06:24 PM


All times are GMT +5.5. The time now is 05:39 AM.