[英]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: 笔记:
Best. 最好。
The main changes I would make to your code are to 我将对您的代码进行的主要更改是
return
statement since that will exit our method prematurely return
语句,因为那样会过早退出我们的方法 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>
存储您的双精度数(您不必提前声明大小,这很好,因为我们不确定字符串中有多少项可以成功转换为双精度数) 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.24976,0.6598,0.1666,0.24976,497.42,0.73378,");
input.Append("2.6349,0.24976,0.14942,43.37,1.2479,0.21402,0.11998,0.47706,");
input.Append("0.50494,0.60411,1.4582,1.7615,5.9443,0.11788,593.27,0.50591,");
input.Append("0.12804,0.66295,0.14942,94.14,3.8772,0.56393,0.21402,1.741,");
input.Append("1.5225,49.394,0.1853,0.11085,2.042,0.051402,0.12804,114.42,");
input.Append("71.05,1.0097,348690,0.12196,0.39718,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.