简体   繁体   中英

Split and Convert string to double C#

I want to convert string to double.

Here's example of what I do :

string line = "4.1;4.0;4.0;3.8;4.0;4.3;4.2;4.0;"; 

double[] values = line2.Split(';').Select(double.Parse).ToArray();

But an error appears

Input string was not in a correct format.

When I try

string line2 = "1;2;3;4;5;6;7;8;9;10;11;12";

double[] values = line2.Split(';').Select(double.Parse).ToArray();

It works perfectly fine.

What should be input format for double values to work ?

Your problem is the last semicolon in the first input. The double.Parse method is being passed an empty string. double value2 = double.Parse(""); There are several ways to fix this, I'll outline two here:

  1. Check if the last character in the input is a semicolon, if so, strip it. (This should be self explanatory.)

  2. Use the StringSplitOptions.RemoveEmptyEntries overload.

I prefer the second option, myself. As this also removes issue with two consecutive semicolons.

string line = "4.1;4.0;4.0;3.8;4.0;;4.3;4.2;4.0;";
double[] values = line.Split(new char[]{';'}, StringSplitOptions.RemoveEmptyEntries).Select(double.Parse).ToArray();

Also, just to humour the idea that it could be a culture issue as well; the following code makes adjustments for culture-specific scenarios. The reason for the if check is to save on compute time. It could be removed if you desire, with no harm to the overall affect. (It simply means that the programme will replace . with . in situations where the programme is run on a computer with a culture set to use decimals for separators. This is merely a simple optimization.)

string line = "4.1;4.0;4.0;3.8;4.0;;4.3;4.2;4.0;";

if (CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator != ".")
    line = line.Replace(".", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);

double[] values = line.Split(new char[]{';'}, StringSplitOptions.RemoveEmptyEntries).Select(s => double.Parse(s)).ToArray();

Another sidebar about the potential for a culture issue: if it were a culture issue, the operation would not have thrown an exception, but instead simply return each digit of the number without separator. (Ie 41, 40, 40, 38, 40, 43, 42, 40 )

Other options:

  1. Use double.TryParse instead.

  2. Use a custom for loop and manually parse your data.

There are likely other options as well that I cannot think of.

Another option would be to use the double.TryParse() method on each item in your split array. This will ensure that each item in the array (empty or not) is a valid double before attempting to add it to the values array.

For example:

string line = "4.1;4.0;4.0;3.8;4.0;4.3;4.2;4.0;";

double temp = 0;

double[] values = line.Split(';')
    .Where(item => double.TryParse(item, out temp))
    .Select(i => temp).ToArray();

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