我有一个字符串,如45,5235234096284或112,013574120648。我想把它转换成双倍。我试过以下代码,但我得到错误'System.Globalization.CultureInfo'不包含'GetCultureInfo的定义 '因为框架版本。我无法改变框架选项。我已经看了另一个解决方案,但我无法理解清楚。用逗号将这些字符串转换为双倍的最佳方法是什么。
var convertedMapPoint = double.Parse(mapPoint, CultureInfo.GetCultureInfo(1053));
答案 0 :(得分:2)
首先,您应该在空格上分割输入,因为那是分隔符:
string input = "45,5235234096284 112,013574120648";
string[] splitted = input.Split(' ');
然后你可以迭代拆分的结果,并将每个转换为double:
List<double> doubles = new List<double>();
foreach (string s in splitted)
{
doubles.Add(double.Parse(s, new CultureInfo("fr-FR")));
}
我使用fr-fr
,因为它有逗号作为小数点分隔符,但您可以使用任何文化来执行此操作。
如果这对你不起作用,并且你100%确定没有.
千分隔符,你可以使用它:
doubles.Add(double.Parse(s.Replace(",", ".")));
然后,您可能希望将不变文化添加为与平台无关:
doubles.Add(double.Parse(s.Replace(",", ".", CultureInfo.InvariantCulture)));
答案 1 :(得分:0)
您应该将字符串拆分为由Space分隔的数组,然后转换每个部分。
string mapPoint = "45,5235234096284 112,013574120648";
var mapPoints = mapPoint.Split(' ');
var convertedMapPointX = double.Parse(mapPoints[0], CultureInfo.GetCultureInfo(1053));
var convertedMapPointY = double.Parse(mapPoints[1], CultureInfo.GetCultureInfo(1053));
Console.WriteLine("X: {0}\tY: {1}", convertedMapPointX, convertedMapPointY);
<强>输出:强>
X: 45.5235234096284 Y: 112.013574120648
答案 2 :(得分:0)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StringToMapPoint
{
class Program
{
static void Main(string[] args)
{
string mapPointStr = "45,5235234096284 112,013574120648";
MapPoint mp = mapPointStr.ToMapPoint();
Console.WriteLine(mp.ToString());
}
}
public class MapPoint
{
public double X { get; set; }
public double Y { get; set; }
public override string ToString()
{
return "X: " + X + " Y: " + Y;
}
}
public static class MapPointHelper
{
public static MapPoint ToMapPoint(this string str)
{
var split = str.Replace(".", ",").Split(' ');
return new MapPoint() { X = Double.Parse(split[0]), Y = Double.Parse(split[1]) };
}
}
}