gpt4 book ai didi

c# - 千位分隔值到整数

转载 作者:太空狗 更新时间:2023-10-29 17:31:23 25 4
gpt4 key购买 nike

我想将一千个分隔值转换为整数,但遇到一个异常。

double d = Convert.ToDouble("100,100,100"); 

工作正常并获得 d=100100100

int n = Convert.ToInt32("100,100,100");

正在获取一种格式异常

Input string was not in a correct format

为什么?

最佳答案

试试这个:

int i = Int32.Parse("100,100,100", NumberStyles.AllowThousands);

请注意,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
}

关于c# - 千位分隔值到整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6139604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com