C# 将包含浮点数的字符串转换为整数
例如,获取可以为空或包含“1.2”的字符串并将其转换为整数的最佳方法是什么? int.TryParse
当然会失败,我不想使用 float.TryParse
然后转换为 int
。
What is the best way to take a string which can be empty or contain "1.2" for example, and convert it to an integer? int.TryParse
fails, of course, and I don't want to use float.TryParse
and then convert to int
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
解决方案 1:
Convert.ToDouble
(取决于文化)您可以使用
Convert.ToDouble
。但是,要小心!仅当当前区域性设置中的数字分隔符是句点字符时,以下解决方案才有效。解决方案 2:
Convert.ToDouble
(文化无关)最好使用
IFormatProvider
并以独立于当前区域性设置的方式转换数字:解决方案 3:解析和转换Split
完成此任务的另一种方法是对解析的字符串使用 Split:
或者:
注意
IsNullOrEmpty
条件。NumberFormatInfo.NumberDecimalSeparator
属性。Parse
、TryParse
或明智地转换
类。阅读更多内容:Solution 1:
Convert.ToDouble
(culture-dependent)You may use
Convert.ToDouble
. But, beware! The below solution will work only when the number separator in the current culture's setting is a period character.Solution 2:
Convert.ToDouble
(culture-independent)It's preferable to use
IFormatProvider
and convert the number in an independent way from the current culture settings:Solution 3: Parse & Split
Another way to accomplish this task is to use Split on parsed string:
Or:
Notes
IsNullOrEmpty
condition.NumberFormatInfo.NumberDecimalSeparator
property.Parse
,TryParse
orConvert
class wisely. Read more at:我不知道解析为
float
并转换为int
有什么问题。我怀疑任何其他方式都会更有效,但这里有一个尝试:为了全球化代码,您需要将
"."
替换为System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
。I don't know what's wrong with parsing to a
float
and converting to anint
. I doubt that any other way would be more efficient but here's an attempt:In order to globalize the code you would need to replace
"."
withSystem.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
.您需要先对其进行舍入,除非您希望将 0.9f 转换为 0 而不是 1。
You need to round it first unless you want 0.9f being converted to 0 instead of 1.
也许您可以尝试使用字符串函数删除浮点之后的所有内容,然后转换为 int。但说实话,我认为这并不比转换为 float 然后再转换为 int 更好。
Maybe you can try to delete everything after floating point using string functions and then convert to int. But seriously I don't think it's better than converting to float and then to int.
我认为另一种方法是将字符串分割成小数点(.)作为分隔符,然后解析整数。当然,我还没有问您该字符串是否可能包含类似
“37.56 英里 in 32.65 秒”
类型的值。考虑到字符串中只有一个值(字符串或数字),我可以想到以下行中的内容:
I think another way of doing it would be splitting the string into pieces taking the decimal (.) as the delimiter and then parsing for the integer. Of course, I am yet to ask you if the string might contain values like
"37.56 miles in 32.65 seconds"
type values.Considering there will be only one value (string or number) in the string, I can think of something in the following line:
您可以使用 Visual Basic 运行时库在 C# 中完成此操作。
您需要将对程序集 Microsoft.VisualBasic.dll 的引用添加到您的解决方案中。
然后以下代码将完成您的转换:
You can use the Visual Basic runtime Library to accomplish this from c#.
You need to add a reference to the assembly Microsoft.VisualBasic.dll to your solution.
Then the following code will do your conversion:
我遇到了同样的问题,最终使用了 Mark 和 Dariusz 的混合体:
I had this same problem and ended up using a hybrid of Mark's and Dariusz':