Convert String to float

Hey C# Programmers,

I think I have a rather easy question, 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????

I've been appending numbers into a label called lblDisplay and I want to store them in to a float variable called Value, so I tried the following and recieved compilation errors.

Value = lblDisplay.Text;

Thanks.
Calvin0380Asked:
Who is Participating?
 
bgungorConnect With a Mentor Commented:
Use:

Value = System.Convert.ToSingle(lblDisplay.Text);

The Convert class converts to and from many different data types.

Hope this helps,

Bg
               
0
 
Calvin0380Author Commented:
also if it helps I'm usuing Visual Studio .NET
0
 
soniicCommented:
Hello,

I dont think C# has an actual "Float" datatype that you can convert to, however, you can still use either "Decimal", "Single" or "Double".

The following code will work:

double value = System.Convert.ToDouble(lblDisplay.Text);

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Mohammed NasmanSoftware DeveloperCommented:
Hello

  Use Double datatype in C# to work with float numbers

also there's a static method found in most of datatype classes to convert the string value to corresponding numberic value

  Double value = Double.Parse(lblDisplay.Text);

Best regards
Mohammed Nasman
0
 
Calvin0380Author Commented:
Thankyou, this worked!
0
 
spikewolfCommented:
Greetings,
   Yeah, the single and the double are C#'s version of floating point numbers.  Each variable type, a class go figure, has its own methods.  Check out the parse ones suggested above.  They are really helpful in changing the type of information.
Double.Parse
Single.Parse
Int32.Parse, etc.
It changes your variable type for ya :)
Laters
Spikewolf
0
All Courses

From novice to tech pro — start learning today.