使用C#将字符串转换为float [英] Convert string to float using c#
本文介绍了使用C#将字符串转换为float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将字符串转换为浮点数.
我必须使用它来设置Label的大小
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)));
推荐答案
这不是转换"(这是CodeProject查询者的真实诅咒,我不知道为什么),这是解析" :
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 [ ^ ].
是的,对于大多数应用程序,您确实要使用double
,而不是Single
.对于System.Single
(别名float
),所有方法都相同:
http://msdn.microsoft.com/en-us/library/system.single.aspx [ ^ ].
方法的尝试…"形式的不同之处在于,它们永远不会引发异常. (它们不会抛出异常,然后按照我的想法捕获,它们根本不会抛出;我通过源代码检查了该实现细节.)相反,您检查返回值以查看是否解析成功与否.
—SA
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 usedouble
, notSingle
. ForSystem.Single
(aliasfloat
), 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
这篇关于使用C#将字符串转换为float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文