且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将字符串转换在C#翻番

更新时间:2023-02-12 15:32:17

在的情况下,你的第一个的的(如你要求它):这似乎是文化相关的问题,试试这个它应该工作(更换 ):

  Console.WriteLine(Convert.ToDouble(52,8725945));

在这种情况下,你应该例如解析您的字符串 double.Parse

  double.Parse(52.8725945,System.Globalization.CultureInfo.InvariantCulture);

在另一方面,你在你的字符串后面有不正确的两倍。

I have a string with double-type values ("value1#value2#value3#...). I split it to string table. Then i want to convert an element from this table to double, and I get a n error. What is wrong? How can I get from this string (string a) values in double type?

string a = "52.8725945#18.69872650000002#50.9028073#14.971600200000012#51.260062#15.5859949000000662452.23862099999999#19.372202799999250800000045#51.7808372#19.474096499999973#";
string[] tablicaLatLng = a.Split(new char[] { '#' });
for (int i = 0; i < tablicaLatLng.Length; i++)
{
  Console.WriteLine(tablicaLatLng[i]); 
}
Console.WriteLine(tablicaLatLng[0]); // 52.8725945
Convert.ToDouble(tablicaLatLng[0]); // error

In case of your first double (as you ask for it): it seems to be culture related problem, try this it should work (replacing . with ,):

Console.WriteLine(Convert.ToDouble("52,8725945"));

In such a case you should parse your string e.g. double.Parse:

double.Parse("52.8725945", System.Globalization.CultureInfo.InvariantCulture);

On the other hand you have incorrect double later in your string.