简体   繁体   中英

parsing a string with a double and getting error input string was not in correct format

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

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM