?
Solved

Convert String to float

Posted on 2003-02-19
6
Medium Priority
?
5,556 Views
Last Modified: 2007-11-27
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.
0
Comment
Question by:Calvin0380
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 

Author Comment

by:Calvin0380
ID: 7982247
also if it helps I'm usuing Visual Studio .NET
0
 
LVL 4

Accepted Solution

by:
bgungor earned 200 total points
ID: 7982375
Use:

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

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

Hope this helps,

Bg
               
0
 

Expert Comment

by:soniic
ID: 7983576
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 7986691
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
 

Author Comment

by:Calvin0380
ID: 7991434
Thankyou, this worked!
0
 

Expert Comment

by:spikewolf
ID: 7994087
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

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question