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 ','.

3
  • Use String.Replace or other regional setings where decimal separtor is .. Commented May 8, 2017 at 11:47
  • Why do you want to use float typecasting? Commented May 8, 2017 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 Stack Overflow[1] question. I hope this solves your problem. [1]: stackoverflow.com/questions/9524682/… Commented May 8, 2017 at 11:52

2 Answers 2

3

Use

//(float)Convert.ToDouble(words[1]),
  (float)Convert.ToDouble(words[1], CultureInfo.InvariantCulture),
Sign up to request clarification or add additional context in comments.

Comments

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));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.