Monday 3 September 2007

System Convert Class

As they say, every day is a school day. I was getting an "Invalid Format Exception" when trying to read a VAT rate from a config file and convert it to a decimal using Convert.ToDecimal(). It turns out that the Convert class by default uses
CurrentCulture.NumberFormat.CurrencyDecimalSeparator


I was testing a website and i had my CurrentCulture set to French which meant the value of 0.175 for the VAT rate was no longer valid as in France the currencyDecimalSeperator is ',' not '.'

Because our config entries will all be in English i created a NumberFormatInfo object which could then be passed into the Convert method call and it all works fine.

No comments: