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

josh
  • 378
  • 6
  • 20
  • 1
    Friendly reminder not to use `double` for currency. Use `decimal`. [See here](https://stackoverflow.com/a/1165788/1563833) – Wyck Apr 07 '21 at 13:21

2 Answers2

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.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
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.

tymtam
  • 31,798
  • 8
  • 86
  • 126