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?
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With