简体   繁体   English

输入的字符串格式不正确(双精度)

[英]Input string was not in a correct format (double)

I'm trying to convert a string (ex. "0.20055") to its equivalent double. 我正在尝试将字符串(例如“ 0.20055”)转换为其等效的双精度型。

I already know that for some reason that string does not contain a valid double, but I don't understand why. 我已经知道,由于某种原因,字符串不包含有效的double,但是我不明白为什么。

I really need to be able to convert this string to a double... Could anyone explain me why it's not a valid double? 我真的需要能够将此字符串转换为双精度型。有人可以解释一下为什么它不是有效的双精度型吗? And how to fix this? 以及如何解决这个问题?

Here is some of my code: 这是我的一些代码:

double[] values_in_double = null;
string[] values_in_string = lines[j].Split(',');
for(int x=0; x < numCols; x++)
{
    double val;
    bool r = double.TryParse(values_in_string[x],out val);
    if (!r)
        return;
    values_in_double[x] = Convert.ToDouble(values_in_string[x]);

}

As you can see I have a string[] of lines, in which a line is like: 如您所见,我有一行的string[] ,其中一行类似于:

0.20055,0.37951,0.39641,2.0472,32.351,0.38825,0.24976,1.3305,1.1389,0.50494 0.20055,0.37951,0.39641,2.0472,32.351,0.38825,0.24976,1.3305,1.1389,0.50494

and I split them. 我把它们分开。 But then I need to convert each one to a double value. 但是然后我需要将每个转换为双精度值。

Try this 尝试这个

Double.TryParse(values_in_string[x], NumberStyles.Any, CultureInfo.InvariantCulture, out val); Double.TryParse(values_in_string [x],NumberStyles.Any,CultureInfo.InvariantCulture,输出值);

change 更改

double[] values_in_double = null;

    string[] values_in_string = lines[j].Split(',');

to

string[] values_in_string = lines[j].Split(',');
int arraySize = values_in_string.Length;
double[] values_in_double = new double[arraySize];

as you use 当您使用

double[] values_in_double = null;

and value cannot be assign to null array 并且值不能分配给空数组

Try changing the line : 尝试更改行:

double[] values_in_double = null;

into

double[] values_in_double = new double[numCols];

Because otherwise the code you provided wouldn't run since the array is not defined (cannot be null). 因为否则,您提供的代码将无法运行,因为未定义数组(不能为null)。

The problem you have with that is the culture definition on your PC or server, if the delimitator are ',' the sistem interpret '.' 您遇到的问题是PC或服务器上的区域性定义,如果定界符为',则系统解释为'。 as a char. 作为一个字符。

Use like this: 像这样使用:

        double vValue;
        string[] vValuesString = vLines[j].Split(',');
        double[] vValuesDouble = new double[vValuesString.Length];

        for (int x = 0; x < vValuesString.Length; x++)
            if (double.TryParse(vValuesString[x], System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out vValue))
                return; 'or use' continue;'or use' vValuesDouble[x] = 0;
            else vValuesDouble[x] = vValue;

NOTES: 笔记:

  • Instance your 'double[] vValuesDouble = new double[vValuesString.Length];', you can't asing a value to a null array. 实例您的'double [] vValuesDouble = new double [vValuesString.Length];',您无法将值赋值给空数组。
  • When you use a TryParce the out var return with the result of convert string to a double, you don't need to re-convert it again. 当您使用TryParce out var返回值并将转换字符串的结果转换为double时,则无需再次将其转换。
  • Never declare variable in a loop, it get declare and instace every run of loop. 永远不要在循环中声明变量,它会在每次循环时声明并实例化。
  • TryParce return a bool value that indicates the result of convert you don't need declare and instance a var, just use it in a if. TryParce返回一个布尔值,该值指示您无需声明和实例化var的转换结果,只需在if中使用它即可。
  • Use some protocol to define your variable's name. 使用某种协议来定义变量的名称。
  • Try to resume your code the most posible you can. 尽量恢复您的代码。
  • I put the entire using System.Globalization, but you can declare the using on top or define your System.Globalization for your entire aplication. 我将整个using放置在System.Globalization中,但是您可以在顶部声明using,或者为整个应用程序定义System.Globalization。
  • And last, sorry for my english, is not my first languaje. 最后,对不起我的英语,不是我的第一个语言。

Best. 最好。

The main changes I would make to your code are to 我将对您的代码进行的主要更改是

  1. remove the return statement since that will exit our method prematurely 删除return语句,因为那样会过早退出我们的方法
  2. use a List<double> to store your doubles (you don't have to declare a size ahead of time, which is nice because we don't know for sure how many items in the string will successfully convert to a double) 使用List<double>存储您的双精度数(您不必提前声明大小,这很好,因为我们不确定字符串中有多少项可以成功转换为双精度数)
  3. to use the val variable when adding it to the List , since double.TryParse has assigned the value to this variable already. 在将val变量添加到List时使用val变量,因为double.TryParse已经为该变量分配了值。

I also added a check to ensure that the variable numRows is not larger than the string array, because that would cause an exception in our loop: 我还添加了一项检查,以确保变量numRows不大于字符串数组,因为这会在我们的循环中引起异常:

// Mock values read from file
var input = new StringBuilder();
input.Append("0.20055,0.37951,0.39641,2.0472,32.351,0.38825,0.24976,1.3305‌​,");
input.Append("1.1389,0.50494,0.24‌​976,0.6598,0.1666,0.‌​24976,497.42,0.73378‌​,");
input.Append("2.6349,0.24976,0.14‌​942,43.37,1.2479,0.2‌​1402,0.11998,0.47706‌​,");
input.Append("‌​0.50494,0.60411,1.4‌​582,1.7615,5.9443,0.‌​11788,593.27,0.50‌​591,");
input.Append("0.12804,0.66295,0.14942,94.14,‌​3.8772,0.56393,0.214‌​02,1.741,");
input.Append("1.5‌​225,49.394,0.1853,0.‌​11085,2.042,0.051402,0.12804,114‌​.42,");
input.Append("71.05,1.0097,348690,0.12196,0.39‌​718,0.87804,0.37854,‌​0.25792,");
input.Append("2.2437,2.248‌​,0.001924‌​,8.416,5.1372,82.658‌​,4.4158,7.4277");

string[] values_in_string = inputString.ToString().Split(',');

// Declare values_in_double as a List so we don't have to worry about sizing
List<double> values_in_double = new List<double>();

// We will get errors if numCols is larger than our string array, so to be safe,
// set a variable that is the smallest of either numCols or our string array length
var numIterations = Math.Min(numCols, values_in_string.Length);

for (int x = 0; x < numIterations; x++)
{
    double val;

    // If TryParse succeeds, 'val' will contain the double value, so add it to our List
    if (double.TryParse(values_in_string[x], NumberStyles.Any, 
        System.Globalization.CultureInfo.InvariantCulture, out val))
    {
        values_in_double.Add(val);
    }
}

There is also a much shorter way to write this code. 还有一种更短的编写此代码的方法。 The line basically says: For each item in values_in_string , Where double.TryParse returns true , Select val (the item as a value) And convert all the val items into a List : 该行基本上说:对于values_in_string每个项目,其中double.TryParse返回true ,选择val (将该项目作为值)并将所有val项目转换为List

string[] values_in_string = inputString.ToString().Split(',');
double val = 0;

List<double> values_in_double = values_in_string
    .Where(value => double.TryParse(value, NumberStyles.Any, 
        System.Globalization.CultureInfo.InvariantCulture, out val))
    .Select(v => val)
    .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 输入的字符串格式不正确将字符串加倍[] []数组 - Input string was not in a correct format String to double [] [] array 将字符串转换为double:输入字符串的格式不正确 - Convert string to double: input string was not in a correct format double.Parse中的输入字符串格式不正确 - Input string was not in a correct format in double.Parse 输入字符串的格式不正确。将String转换为double - Input string was not in a correct format. Converting String to double 尝试将字符串解析为double,得到“输入字符串格式不正确”。 - Trying to parse a string to a double, getting “Input string was not in a correct format.” 将字符串转换为双精度时,输入字符串的格式不正确 - Input string was not in a correct format when convert string to double 用双精度解析字符串并获取错误输入字符串格式不正确 - parsing a string with a double and getting error input string was not in correct format 使用双精度小数时输入字符串格式不正确 - Input string not in correct format when using Double for Decimals 输入的字符串格式不正确,“ double.parse(textbox.text);” - Input string was not in a correct format, “double.parse(textbox.text);” Double ParseDouble - 输入字符串格式不正确 - 如何修复? - Double ParseDouble - Input string was not in a correct format - How to Fix?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM