Tuesday 15 June 2010

c# - string.Format(…, double) followed by double.Parse using same NumberFormatInfo results in FormatException. Why? -



c# - string.Format(…, double) followed by double.Parse using same NumberFormatInfo results in FormatException. Why? -

numberformatinfo nfi = new numberformatinfo() { currencysymbol = "$$s. ", currencygroupseparator = ".", currencydecimalseparator = ",", negativesign = "-", currencynegativepattern = 2 }; double amount = double.parse("$$s. 1.123,00", nfi);

the lastly line throws formatexception, , don't know why. string i'm trying parse comes this:

string.format(nfi, "{0:c}", 1123.00)

you're not telling it should take currency value. that, need phone call overload accepts numberstyles value, , include numberstyles.allowcurrencysymbol. example:

using system; using system.globalization; class test { static void main() { numberformatinfo nfi = new numberformatinfo() { currencysymbol = "$$s. ", currencygroupseparator = ".", currencydecimalseparator = ",", negativesign = "-", currencynegativepattern = 2 }; double d = double.parse("$$s. 1.123,00", numberstyles.number | numberstyles.allowcurrencysymbol, nfi); console.writeline(d); } }

note currency values improve represented decimal double though.

c# string double string-parsing string.format

No comments:

Post a Comment