将字符串转换在C#翻番 [英] Converting string to double in C#

查看:101
本文介绍了将字符串转换在C#翻番的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个double型的值(值1#VALUE2#VALUE3#...)的字符串。
我把它拆分到字符串表。然后我想一个元素从该表转换为翻一番,我得到一个n错误。哪里不对?我怎样才能从这个字符串得到(字符串)在double类型的值?

 字符串= \"52.8725945#18.69872650000002#50.9028073#14.971600200000012#51.260062#15.5859949000000662452.23862099999999#19.372202799999250800000045#51.7808372#19.474096499999973#\";
字符串[] = tablicaLatLng a.Split(新的char [] {'#'});
的for(int i = 0; I< tablicaLatLng.Length;我++)
{
  Console.WriteLine(tablicaLatLng [I]);
}
Console.WriteLine(tablicaLatLng [0]); // 52.8725945
Convert.ToDouble(tablicaLatLng [0]); //错误


解决方案

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

  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.

这篇关于将字符串转换在C#翻番的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆