当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现将千分位字符串转换成数字的方法

C#实现将千分位字符串转换成数字的方法

2019年07月18日  | 移动技术网IT编程  | 我要评论

玛丽莲梦露图片,临淄中学,张君龙28秒ko泰拳王

本文实例主要实现了c#将千分位字符串转换成数字的方法,对c#初学者而言有一定的借鉴价值,主要内容如下:

主要功能代码如下:

/// <summary>
/// 将千分位字符串转换成数字
/// 说明:将诸如"–111,222,333的千分位"转换成-111222333数字
/// 若转换失败则返回-1
/// </summary>
/// <param name="thousandthstr">需要转换的千分位</param>
/// <returns>数字</returns>
public static int parsethousandthstring(this string thousandthstr)
{
  int _value = -1;
  if (!string.isnullorempty(thousandthstr))
  {
 try
 {
   _value = int.parse(thousandthstr, numberstyles.allowthousands | numberstyles.allowdecimalpoint | numberstyles.allowleadingsign);
 }
 catch (exception ex)
 {
   _value = -1;
   debug.writeline(string.format("将千分位字符串{0}转换成数字异常,原因:{0}", thousandthstr, ex.message));
 }
  }
  return _value;
}

单元测试如下:

[testmethod()]
public void parsethousandthstringtest()
{
  string _thousandthstr = "-111,222,333";
  int _expected1 = -111222333;
  int _actual1 = stringtoolv2.parsethousandthstring(_thousandthstr);
  assert.areequal(_expected1, _actual1);
}

感兴趣的读者可以自己测试一下,希望对大家学习c#有所帮助!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网