My application reads an Excel file using VSTO and adds read data to a StringDictionary
. It adds only data that is a number with few digits (1000 1000,2 1000,34 - comma is a delimiter in Russian standards).
What is better to check current string being appropriate number?
object data, string key; // data had read
try
{
Convert.ToDouble(regionData, CultureInfo.CurrentCulture);
dic.Add(key, regionData.ToString());
}
catch (InvalidCastException)
{
// is not a number
}
or
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 if Dictionary<string, double>
because of following parsing algorithm issues.
My questions: which way is faster? is more safe?
And is it better to call Convert.ToDouble(object)
or Convert.ToDouble(string)
?