Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to convert string to float.

i have to use it to set the size of Label
this.lblComanyName.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
Posted
Comments
[no name] 9-Oct-12 16:07pm    
did you try float.Parse()?
Sergey Alexandrovich Kryukov 9-Oct-12 16:08pm    
I don't believe so, so I decided to answer...
--SA
Sergey Alexandrovich Kryukov 9-Oct-12 16:07pm    
As if MSDN was broken, honestly...
--SA

1 solution

This not "convert" (this word is a real curse of CodeProject inquirers, I don't know why), this is "parse":
http://msdn.microsoft.com/en-us/library/fd84bdyt.aspx[^],
http://msdn.microsoft.com/en-us/library/7yd1h1be.aspx[^],
http://msdn.microsoft.com/en-us/library/t9ebt447.aspx[^],
http://msdn.microsoft.com/en-us/library/0kb11ck8.aspx[^],
http://msdn.microsoft.com/en-us/library/994c0zb1.aspx[^],
http://msdn.microsoft.com/en-us/library/3s27fasw.aspx[^].

And yes, for most applications you really want to use double, not Single. For System.Single (alias float), all methods are the same:
http://msdn.microsoft.com/en-us/library/system.single.aspx[^].

The difference the "Try…" forms of methods make is that they do not throw exceptions, ever. (They do not throw and then catch as I thought, they don't throw at all; I checked up this implementation detail by source code.) Instead, you check up the return values to see if the parsing is successful or not.

—SA
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900