千位分隔值为整数

nik*_*owj 9 c#

我想将一千个分隔值转换为整数,但我得到一个例外.

double d = Convert.ToDouble("100,100,100"); 
Run Code Online (Sandbox Code Playgroud)

工作正常,越来越好 d=100100100

int n = Convert.ToInt32("100,100,100");
Run Code Online (Sandbox Code Playgroud)

得到一个格式异常

输入字符串的格式不正确

为什么?

mat*_*att 26

试试这个:

int i = Int32.Parse("100,100,100", NumberStyles.AllowThousands);
Run Code Online (Sandbox Code Playgroud)

请注意,该Parse方法将在无效字符串上引发异常,因此您可能还想检查该TryParse方法:

string s = ...;
int i;
if (Int32.TryParse(s, NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out i))
{
    // if you are here, you were able to parse the string 
}
Run Code Online (Sandbox Code Playgroud)