eyalb eyalb - 26 days ago 10
ASP.NET (C#) Question

Converting string of decimal to list

When tring to convert string of decimal
"0,0,0,0,0,0,8555,127875,-180000,152000,55000,3.84,648000"
I get an error when one of the items is negative

Input string was not in a correct format.

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();

Answer

Add System.Globalization.NumberStyles.AllowLeadingSign flag.

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();
Comments