Question

   I have changed Global Application Culture thread for currency number format in (fr-CA) as shown below. 

Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyPositivePattern = 1;
                                Thread.CurrentThread.CurrentUICulture.NumberFormat.CurrencyNegativePattern = 5;

                            CultureInfo CADCultureref = new CultureInfo("fr-CA");
                            CADCultureref = Thread.CurrentThread.CurrentCulture;
                            NumberFormatInfo CADNumFormatref = new NumberFormatInfo();
                            CADNumFormatref = Thread.CurrentThread.CurrentCulture.NumberFormat;

                            CADNumFormatref.CurrencyGroupSeparator = ".";
                            CADNumFormatref.CurrencyDecimalSeparator = ",";
                            CADCultureref.NumberFormat = CADNumFormatref;
                            Thread.CurrentThread.CurrentCulture = CADCultureref;
                            Thread.CurrentThread.CurrentUICulture = CADCultureref;

Then I try to parsing value from currency as shown below:

Decimal digit = 1000000;
String currency = digit.ToString("C"); //  1.000.000,00$ (fr-CAD)
decimal parseValue = decimal.Parse(currency , System.Globalization.NumberStyles.Currency | System.Globalization.NumberStyles.Number); 

I am getting exception "{Input String is not in correct format}" during parsing value.

Unable to parse due to changing in group seperator and decimal seperator in Numberformatinfo of CurrentThread.

I need to show "," in place of "." and "." in place of ",".

Pas de solution correcte

Autres conseils

Unclear what your problem is, but it is generally better to explicitly pass culture information/number format into formatting and parsing functions like:

var  numberFormat = new CultureInfo( "en-US", false ).NumberFormat;
numberFormat.CurrencyDecimalSeparator  = ",";
numberFormat.CurrencyGroupSeparator  = ".";
Console.WriteLine(400000.ToString("C", numberFormat)); // Output: $ 4.000,00   
Console.WriteLine(
    decimal.Parse(400000.ToString("C", numberFormat),
           NumberStyles.Currency | NumberStyles.Number, 
           numberFormat));
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top