将十进制字符串转换为列表

时间:2016-11-07 07:21:30

标签: c# asp.net

当tring转换为十进制字符串时      “0,0,0,0,0,0,8555,127875,-180000,152000,55000,3.84,648000” 当其中一个项目为否定时我收到错误

输入字符串的格式不正确。

System.Globalization.NumberStyles style = System.Globalization.NumberStyles.AllowDecimalPoint | System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.AllowExponent ;
String t = "0,0,0,0,0,0,8555,127875,-180000,152000,55000,3.84,648000";
List<decimal> prices = t.Split(',').Select(n => decimal.Parse(n, style)).ToList();

2 个答案:

答案 0 :(得分:4)

添加System.Globalization.NumberStyles.AllowLeadingSign标志。

System.Globalization.NumberStyles style = System.Globalization.NumberStyles.AllowDecimalPoint | System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.AllowExponent | System.Globalization.NumberStyles.AllowLeadingSign;
String t = "0,0,0,0,0,0,8555,127875,-180000,152000,55000,3.84,648000";
List<decimal> prices = t.Split(',').Select(n => decimal.Parse(n, style)).ToList();

答案 1 :(得分:0)

试试这个

String t = "0,0,0,0,0,0,8555,127875,-180000,152000,55000,3.84,648000";

String[] list = t.Split(',');
List<Decimal> decimals = new List<decimal>();
foreach (string s in list)
{
    decimals.Add(Convert.ToDecimal(s));
}

List<Decimal> decimals = t.Split(',').Select(x => Convert.ToDecimal(x)).ToList();