Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Convert.ToDecimal(string) & Decimal.Parse(string)

Tags:

c#

.net

What is the difference in C# between Convert.ToDecimal(string) and Decimal.Parse(string)?

In what scenarios would you use one over the other?

What impact does it have on performance?

What other factors should I be taking into consideration when choosing between the two?

like image 913
YonahW Avatar asked Sep 18 '08 01:09

YonahW


People also ask

How do you convert a string to a decimal?

Converting a string to a decimal value or decimal equivalent can be done using the Decimal. TryParse() method. It converts the string representation of a number to its decimal equivalent.

How do you convert decimal to double?

You can also convert a Decimal to a Double value by using the Explicit assignment operator. Because the conversion can entail a loss of precision, you must use a casting operator in C# or a conversion function in Visual Basic.


1 Answers

There is one important difference to keep in mind:

Convert.ToDecimal will return 0 if it is given a null string.

decimal.Parse will throw an ArgumentNullException if the string you want to parse is null.

like image 70
James Newton-King Avatar answered Oct 17 '22 09:10

James Newton-King