Skip to main content
GameDev.net gamedev.net
🔒 Locked

String to float in c#?

Started by mits Jul 13, 2006 at 1:17 PM 7 replies 43.4k views
Original Post
mits
mits
Is there a way to convert a string which represents a float number to a float data type or should I have to write a custom function for that? I want to avoid the trouble. Thanks.
SirKnight
SirKnight
string str = "3.14159";float fnum = float.Parse(str);
SamLowry
SamLowry
float.Parse might help you.
kSquared
kSquared
Quote:
Original post by mits
Is there a way to convert a string which represents a float number to a float data type or should I have to write a custom function for that? I want to avoid the trouble. Thanks.


You may want to use TryParse() instead, if this is .NET 2.0. If you can't absolutely 100% guarantee that the string is a float, use TryParse. Otherwise, Parse() will throw an exception.

hope that helps,
- k2 "Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ<
Arild Fines
Arild Fines
Quote:
Original post by kSquared
You may want to use TryParse() instead, if this is .NET 2.0.

Double.TryParse was supported even in 1.x. He could always use that and cast afterwards.

--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
GreenGiant
GreenGiant
Sorry if this looks like I'm hijacking your thread.

I'm currently still learning C#, and in many occasions I have had to convert a string to a float. In most occasions though, I use:

string sNum = "123";float fNum = Convert.ToSingle(sNum);


Is that an incorrect way of doing it?
Ho, ho, ho!
Moe
Moe
Would that convert it into a single instead of a float? I would say you are best off usinig float.Parse().

Mind you, I have limited experience with these things as well.
alexmoura
alexmoura
single and float are the same thing, I believe - VB calls floats singles (or vice versa), and whoever coded Convert used that name. To answer the question, it should work fine, but I think it may throw an exception if the string isn't convertible, so watch out for that. It's likely that internally the parse methods use the convert class.

Let me take a look at the il :)

Edit: according to the IL it actually uses system.Number to do the parsing. Anyway, what you're doing is fine.
Arild Fines
Arild Fines
Quote:
Original post by alexmoura
VB calls floats singles (or vice versa), and whoever coded Convert used that name.

No, that's not the reason. Single is the CLR type name. The base class library needs to be language agnostic, so it makes sense to use the CLR name rather than a C# specific one.

Note that the CLR names can also be used in C#, so the following is valid C#:

Single f = Single.Parse(str);


Quote:

It's likely that internally the parse methods use the convert class.

It's the other way around. Convert.ToSingle calls Single.Parse.

--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.