C# Convert.ToInt32() 带逗号的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1824326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:08:40  来源:igfitidea点击:

Convert.ToInt32() a string with Commas

c#

提问by Emil Davtyan

I have a string that sometimes has commas seperating the number like 1,500and I need to convert this to an Int, currently it is throwing an exception, can someone tell me how to fix this so that sometimes I may input numbers with commas and other times with out commas and it will still convert.

我有一个字符串,有时有逗号分隔数字1,500,我需要将其转换为 Int,目前它正在引发异常,有人可以告诉我如何解决这个问题,以便有时我可以输入带逗号的数字,有时用去掉逗号,它仍然会转换。

采纳答案by CMS

You could use int.Parseand add the NumberStyles.AllowThousandsflag:

您可以使用int.Parse并添加NumberStyles.AllowThousands标志:

int num = int.Parse(toParse, NumberStyles.AllowThousands);

Or int.TryParseletting you know if the operation succeeded:

或者int.TryParse让您知道操作是否成功:

int num;
if (int.TryParse(toParse, NumberStyles.AllowThousands,
                 CultureInfo.InvariantCulture, out num))
{
    // parse successful, use 'num'
}

回答by liya

You can do replace(';', '')before you convert it to int.

您可以replace(';', '')在将其转换为 int 之前执行此操作。

回答by Will

If you don't have to worry about rules for various countries (eg. some use comma as decimal position instead of thousands separator), then just strip out the commas first.

如果您不必担心不同国家/地区的规则(例如,有些使用逗号作为小数位而不是千位分隔符),那么只需先去掉逗号即可。

e.g

例如

string nastyNumber = "1,234";
int result = int.Parse(nastyNumber.Replace(",", ""));

(replace int with double if you need floating point)

(如果需要浮点数,请将 int 替换为 double)

回答by Sam

You can use Decimal.Parse()then cast the result to int. This works with the current culture as well, or you can specify a CultureInfoto use. No need to manually handle commas, decimal points, etc, that's all built in to .NET.

您可以使用Decimal.Parse()then 将结果转换为int. 这也适用于当前文化,或者您可以指定CultureInfo要使用的 a。无需手动处理逗号、小数点等,这些都内置于 .NET 中。

回答by Jon Sagara

You can replace the commas with String.Emptyprior to calling Convert.ToInt32(), or you can look into using Int32.Parse()or Int32.TryParse()with NumberStyles.AllowThousandsas one of the parameters.

您可以String.Empty在调用之前将逗号替换为 with Convert.ToInt32(),或者您可以考虑使用Int32.Parse()Int32.TryParse()withNumberStyles.AllowThousands作为参数之一。

Int32.Parse

Int32.Parse

Int32.TryParse

Int32.TryParse

回答by Daria

What's with the brute force suggestions of removing commas?? What if the string is something like "1,1,1"? Is this a valid number? Removing commas would make it a valid number and make the conversion semantically incorrect.

删除逗号的蛮力建议是什么?如果字符串类似于“1,1,1”怎么办?这是一个有效的数字吗?删除逗号将使其成为有效数字并使转换在语义上不正确。

There's a reason NumberStyles exist and the Int.Parse() method can parse with or without regard to the number style.

NumberStyles 存在是有原因的,Int.Parse() 方法可以在不考虑数字样式的情况下进行解析。

回答by Simpa

You can use the code

你可以使用代码

int num = Strings.Replace(str, ",", string.Empty);

This replaces all occurrences of "," with "" (empty string). So 1,000,000 turns 1000000

这会将所有出现的“,”替换为“”(空字符串)。所以 1,000,000 变成 1000000