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

前端 未结 7 1792
傲寒
傲寒 2020-12-10 10:18

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

In what scenarios would you use one over the othe

7条回答
  •  春和景丽
    2020-12-10 10:42

    Knowing that Convert.ToDecimal is the way to go in most cases because it handles NULL, it, however, does not handle empty string very well. So, the following function might help:

    'object should be a string or a number
    Function ConvertStringToDecimal(ByVal ValueToConvertToDecimal As Object) As Decimal
        If String.IsNullOrEmpty(ValueToConvertToDecimal.ToString) = False Then
            Return Convert.ToDecimal(ValueToConvertToDecimal)
        Else
            Return Convert.ToDecimal(0)
        End If
    End Function
    

提交回复
热议问题