1

I have a string from a csv file that looks like this ""711,200.00"" I am trying to convert that number to a double with this code

collaterel.LoanQty = double.Parse(values[25], CultureInfo.InvariantCulture);

I have taken out the comma and tried to convert to a double and I still get input string was not in correct format

This is what I used to take out the comma

  if (values[25].Contains(","))
  {
     values[25] = values[25].Replace(",", "");
  }

I have tried many culture this still fails.

screen shots double.parse with cultureinfo does not work

enter image description here

enter image description here

enter image description here

1
  • 1
    Friendly reminder not to use double for currency. Use decimal. See here Commented Apr 7, 2021 at 13:21

2 Answers 2

5

Since double.Parse("711,200.00", CultureInfo.InvariantCulture) works, but your input is ""711,200.00"", you just need to trim the quotes:

collaterel.LoanQty = double.Parse(values[25].Trim('"'), CultureInfo.InvariantCulture);

In general you should use double.TryParse to avoid catching exceptions with invalid formats.

Sign up to request clarification or add additional context in comments.

1 Comment

You addressed the real issue, the one that was ignored by me :)
1

Use double.Parse.

var s = "711,200.00";

Console.WriteLine(double.Parse(s, System.Globalization.NumberStyles.Number));

prints

711200

NumberStyles.Number is

Number 111

Indicates that the AllowLeadingWhite, AllowTrailingWhite, AllowLeadingSign, AllowTrailingSign, AllowDecimalPoint, and AllowThousands styles are used. This is a composite number style.

2 Comments

the type isn't a decimal its a double
No drama, let's make it double.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.