Jump to content

  • Log In with Google      Sign In   
  • Create Account

String to float in c#?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 mits   Members   -  Reputation: 122

Like
0Likes
Like

Posted 13 July 2006 - 07:17 AM

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.

Sponsor:

#2 SirKnight   Members   -  Reputation: 316

Like
0Likes
Like

Posted 13 July 2006 - 07:23 AM



string str = "3.14159";
float fnum = float.Parse(str);




#3 SamLowry   Members   -  Reputation: 1676

Like
0Likes
Like

Posted 13 July 2006 - 07:23 AM

float.Parse might help you.

#4 kSquared   Members   -  Reputation: 1356

Like
0Likes
Like

Posted 13 July 2006 - 07:39 AM

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} | {Blog/Journal} | {e-mail me} | {excellent webhosting}

#5 Arild Fines   Members   -  Reputation: 968

Like
0Likes
Like

Posted 13 July 2006 - 11:20 AM

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]

#6 GreenGiant   Members   -  Reputation: 254

Like
0Likes
Like

Posted 13 July 2006 - 12:52 PM

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?

#7 Moe   Crossbones+   -  Reputation: 1249

Like
0Likes
Like

Posted 13 July 2006 - 01:09 PM

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.

#8 alexmoura   Members   -  Reputation: 450

Like
0Likes
Like

Posted 13 July 2006 - 01:14 PM

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.

#9 Arild Fines   Members   -  Reputation: 968

Like
0Likes
Like

Posted 13 July 2006 - 05:25 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS