Double.TryParse or Convert.ToDouble - which is faster and safer?

c# double tryparse examples
c# converting from string to double
cannot convert from string to double
convert.todouble null
vb net convert string to double
c# double to double
convert varchar to double c#
xamarin forms string to double

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) ?


I did a quick non-scientific test in Release mode. I used two inputs: "2.34523" and "badinput" into both methods and iterated 1,000,000 times.

Valid input:

Double.TryParse = 646ms
Convert.ToDouble = 662 ms

Not much different, as expected. For all intents and purposes, for valid input, these are the same.

Invalid input:

Double.TryParse = 612ms
Convert.ToDouble = ..

Well.. it was running for a long time. I reran the entire thing using 1,000 iterations and Convert.ToDouble with bad input took 8.3 seconds. Averaging it out, it would take over 2 hours. I don't care how basic the test is, in the invalid input case, Convert.ToDouble's exception raising will ruin your performance.

So, here's another vote for TryParse with some numbers to back it up.

Double.TryParse or Convert.ToDouble, Double.TryParse or Convert.ToDouble - which is faster and safer? c# double tryparse examples c# converting from string to double cannot convert from  To parse a string using the formatting information of some other specified culture, use the Double.TryParse(String, NumberStyles, IFormatProvider, Double) method overload. Ordinarily, if you pass the Double.TryParse method a string that is created by calling the Double.ToString method, the original Double value is returned. However, because of


To start with, I'd use double.Parse rather than Convert.ToDouble in the first place.

As to whether you should use Parse or TryParse: can you proceed if there's bad input data, or is that a really exceptional condition? If it's exceptional, use Parse and let it blow up if the input is bad. If it's expected and can be cleanly handled, use TryParse.

Double.TryParse or Convert.ToDouble - which is faster and , Double.TryParse or Convert.ToDouble - which is faster and safer? Question. My application reads an Excel file using VSTO and adds the read data to a  Double.TryParse(String, Double) method works the same as Double.Parse() method except it returns a Boolean value which indicates whether the conversion has been done successfully or not. On success, it returns true and the respected double value gets stored in the ‘out’ parameter. Examples of Convert String to Double in C#


The .NET Framework design guidelines recommend using the Try methods. Avoiding exceptions is usually a good idea.

Convert.ToDouble(object) will do ((IConvertible) object).ToDouble(null);

Which will call Convert.ToDouble(string, null)

So it's faster to call the string version.

However, the string version just does this:

if (value == null)
{
    return 0.0;
}
return double.Parse(value, NumberStyles.Float | NumberStyles.AllowThousands, provider);

So it's faster to do the double.Parse directly.

c# - alternativa más rápida a Convert.ToDouble, Does it exist a faster way to convert a String to Double than Convert. TryParse with specific cached instances of NumberStyles and IFormatProvider (i.e.  To start with, I'd use double.Parserather than Convert.ToDoublein the first place. As to whether you should use Parseor TryParse: can you proceed if there's bad input data, or is that a really exceptional condition? If it's exceptional, use Parseand let it blow up if the input is bad. If it's expected and can be cleanly handled, use TryParse.


If you aren't going to be handling the exception go with TryParse. TryParse is faster because it doesn't have to deal with the whole exception stack trace.

Convert.ToDouble Method (System), Usted puede ahorrar alrededor de 10% llamando Double.TryParse con instancias en caché específicas de NumberStyles y IFormatProvider ToDouble y Double. .com/questions/586436/double-tryparse-or-convert-todouble-which-​is-faster-and-safer?lq=1. La función en esta publicación Faster alternative to decimal. Convert.ToDouble will throw an exception on non-numbers Double.Parse will throw an exception on non-numbers or null Double.TryParse will return false or 0 on any of the above without generating an exception.


I generally try to avoid the Convert class (meaning: I don't use it) because I find it very confusing: the code gives too few hints on what exactly happens here since Convert allows a lot of semantically very different conversions to occur with the same code. This makes it hard to control for the programmer what exactly is happening.

My advice, therefore, is never to use this class. It's not really necessary either (except for binary formatting of a number, because the normal ToString method of number classes doesn't offer an appropriate method to do this).

Doubles (Guava: Google Core Libraries for Java 19.0 API), Converts a specified value to a double-precision floating-point number. Convert.ToDouble Method. Definition. Namespace: System. Assembly: System. Parse(String) method. value is interpreted by using the formatting conventions of the  I'm just beginning to learn C# and the book I'm using is having me make a program to calculate the volume and surface area of a cylinder. The


How to read a double value from the console in C#, Static utility methods pertaining to double primitives, that are not already found in either Double or Returns a string containing the supplied double values, converted to strings as specified by Double. Calling this method is as thread-​safe as calling that method. expressions") public static Double tryParse(String string). It depends on what you expect x to be. If x is a boxed int then (int)x is quickest.. If x is a string but is definitely a valid number then int.Parse(x) is best. If x is a string but it might not be valid then int.TryParse(x) is far quicker than a try-catch block.


Is there a faster way to convert a string to double than Convert. TryParse with specific cached instances of NumberStyles and IFormatProvider (i.e. CultureInfo ): Converting String to Double in C# Converting a string to double might be a very challenging task in programming environments when you consider different culture settings. Most of the time you deal with "."


static void Main(string[] args); {; double a;; try; {; a = Convert. You need to read it as a string than you need to parse it to double. You can read Why C# is called type safe programming language? What is faster in C#, a struct or a class? C# ConvertTo Vs Parse. This article details the benchmark and performance showdown: C# ConvertTo vs Parse methods. In any programming language, data type conversions are inevitably going to happen. C# programmers have quite a few tools up their sleeves thanks to the .Net framework. A few popular methods for each data type include: ConvertTo