我试图将字符串(例如" 0.20055")转换为等效的字符串。
我已经知道由于某种原因,该字符串不包含有效的双精度数,但我不明白为什么。
我真的需要能够将这个字符串转换成双字符串......有人可以解释为什么它不是有效的双重字符串吗?以及如何解决这个问题?
以下是我的一些代码:
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]);
}
如你所见,我有string[]
行,其中一行是:
0.20055,0.37951,0.39641,2.0472,32.351,0.38825,0.24976,1.3305,1.1389,0.50494
我分开了他们。但后来我需要将每一个转换为双倍值。答案 0 :(得分:0)
试试这个
Double.TryParse(values_in_string [x],NumberStyles.Any,CultureInfo.InvariantCulture,out val);
答案 1 :(得分:0)
更改
double[] values_in_double = null;
string[] values_in_string = lines[j].Split(',');
到
string[] values_in_string = lines[j].Split(',');
int arraySize = values_in_string.Length;
double[] values_in_double = new double[arraySize];
当你使用
double[] values_in_double = null;
并且值不能分配给null数组
答案 2 :(得分:0)
尝试更改行:
double[] values_in_double = null;
到
double[] values_in_double = new double[numCols];
因为否则您提供的代码将无法运行,因为未定义数组(不能为null)。
答案 3 :(得分:0)
您遇到的问题是您的PC或服务器上的文化定义,如果定界符是&#39;,&#39;系统解释&#39;。&#39;作为一个角色。
像这样使用:
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;
注意:
最佳。
答案 4 :(得分:0)
我将对您的代码进行的主要更改是
return
语句,因为这会提前退出我们的方法List<double>
存储您的双打(您不必提前声明大小,这很好,因为我们不确定字符串中有多少项会成功转换为双)val
变量添加到List
时使用double.TryParse
变量,因为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);
}
}
不大于字符串数组,因为这会导致循环中出现异常:
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();
:
<!DOCTYPE html>
<html>
<body>
<form method="post" action="javascript:submitNewMessage()" accept-charset="UTF-8" id="msgForm">
<input type="text" name="msg" id="msgField"/>
<input type="submit" name="submit" value="Send"/>
</form>
<script src="jquery.js"></script>
<script>
"use strict";
function submitNewMessage() {
var msg = document.getElementById("msgField").value;
$.post("chatController", {message:msg, author:"${user.username}", gameId:"${game}", chat:"${chat.type}"});
}
</script>
</body>
</html>