0

I have a string array which contains 4 elements. Which looks like this.

enter image description here

How ever, when trying to do this:

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

I get the following error:

'Input string was not in a correct format.'

And that is because it's because the value uses a '.' but if I manually change the array to use a ',' it works. How can I easiest replace all '.' with ','.

Joakim Carlsson
  • 1,490
  • 3
  • 18
  • 41
  • Use `String.Replace` or other regional setings where decimal separtor is `.`. – BWA May 08 '17 at 11:47
  • Why do you want to use `float` typecasting? – Parth Sane May 08 '17 at 11:49
  • `Array.ConvertAll(words.Split(','), Double.Parse);` This will convert your string array to a double array. In fact I got this from another [SO][1] question. I hope this solves your problem. [1]: http://stackoverflow.com/questions/9524682/fastest-way-to-convert-string-array-to-double-array – Parth Sane May 08 '17 at 11:52

2 Answers2

3

Use

//(float)Convert.ToDouble(words[1]),
  (float)Convert.ToDouble(words[1], CultureInfo.InvariantCulture),
Henk Holterman
  • 250,905
  • 30
  • 306
  • 490
2

Try this...

Vector newVector = new Vector(
(float)Convert.ToDouble(words[1], CultureInfo.GetCultureInfo("en-US").NumberFormat),
(float)Convert.ToDouble(words[2], CultureInfo.GetCultureInfo("en-US").NumberFormat));
Mike Gledhill
  • 25,874
  • 6
  • 141
  • 151