我的应用程序使用VSTO读取Excel文件,并将读取的数据加到StringDictionary
.它只添加带有几位数字的数据(StringDictionary
0,StringDictionary
0,2,StringDictionary
0,34-逗号是俄罗斯标准中的分隔符).
判断当前字符串是否为合适的数字更好的方法是什么?
object data, string key; // data had read
try
{
Convert.ToDouble(regionData, CultureInfo.CurrentCulture);
dic.Add(key, regionData.ToString());
}
catch (InvalidCastException)
{
// is not a number
}
或
double d;
string str = data.ToString();
if (Double.TryParse(str, out d)) // if done, then is a number
{
dic.Add(key, str);
}
I have to use StringDictionary
instead of Dictionary<string, double>
because of the following parsing alg或ithm issues.
我的问题是:哪条路更快?哪个更安全?
And is it better to call Convert.ToDouble(object)
或 Convert.ToDouble(string)
?