Decimal.TryParse with Currency input values by ThinqLinq

Decimal.TryParse with Currency input values

I found an issue (or rather the issue recently found me) concerning my advice advocating using Decimal.TryParse on currency items. It turns out if you use the base implementation of Decimal.TryParse, it will not work when presented with currency values by default. Instead, you need to use the overload and specify the NumberStyle explicitly as below:

Dim res As Decimal

If Decimal.TryParse("$1.00", Globalization.NumberStyles.Currency, Nothing, res) Then

Console.WriteLine(res.ToString)

Else

Console.WriteLine("Parse failed")

End If

Console.ReadKey()

Posted on - Comment
Categories: VB -
comments powered by Disqus