将字符串转换为双精度,

问题描述:

我有一个包含4个元素的字符串数组。看起来像这样。将字符串转换为双精度,

enter image description here

如何过,当试图做到这一点:

Vector newVector = new Vector(
(float)Convert.ToDouble(words[1]), 
(float)Convert.ToDouble(words[2])); 

我收到以下错误:

'Input string was not in a correct format.'

这是因为它是因为值使用”。 “但如果我手动更改数组使用','它的作品。 我该如何轻松取代所有'。'与','。

+0

使用'String.Replace'或其他区域setings其中小数separtor是'.'。 – BWA

+0

你为什么要使用'float'类型转换? – Boggartfly

+0

'Array.ConvertAll(words.Split(','),Double.Parse);' 这会将您的字符串数组转换为双数组。其实我从另一个[SO] [1]问题得到了这个。我希望这能解决你的问题。 [1]:http://*.com/questions/9524682/fastest-way-to-convert-string-array-to-double-array – Boggartfly

使用

//(float)Convert.ToDouble(words[1]), 
    (float)Convert.ToDouble(words[1], CultureInfo.InvariantCulture), 

尝试......

Vector newVector = new Vector(
(float)Convert.ToDouble(words[1], CultureInfo.GetCultureInfo("en-US").NumberFormat), 
(float)Convert.ToDouble(words[2], CultureInfo.GetCultureInfo("en-US").NumberFormat));