加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 将数值分隔为整数

发布时间:2020-12-15 06:55:51 所属栏目:百科 来源:网络整理
导读:我想将一千个分离的值转换为整数,但是我得到一个例外. double d = Convert.ToDouble("100,100,100"); 工作正常,得到d = 100100100 int n = Convert.ToInt32("100,100"); 正在获得一个格式异常 Input string was not in a correct format 为什么? 解决方法
我想将一千个分离的值转换为整数,但是我得到一个例外.
double d = Convert.ToDouble("100,100,100");

工作正常,得到d = 100100100

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

正在获得一个格式异常

Input string was not in a correct format

为什么?

解决方法

尝试这个:
int i = Int32.Parse("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 
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读