My application reads an Excel file using VSTO and adds the read data to a StringDictionary
. It adds only data that are numbers with a few digits (1000 1000,2 1000,34 - comma is a delimiter in Russian standards).
What is better to check if the current string is an 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 of Dictionary<string, double>
because of the following parsing algorithm issues.
My questions: Which way is faster? Which is safer?
And is it better to call Convert.ToDouble(object)
or Convert.ToDouble(string)
?