Question

On parse number having group separator and decimal separator showing same output.

For example:

decimal.parse("2,00", Currency) // output is 2
decimal.parse("2.00", Currency) // output is 2

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;

decimal number =  Decimal.Parse("2,00$", System.Globalization.NumberStyles.Currency | System.Globalization.NumberStyles.Number);

// Output is 2 and 
Decimal.Parse("2.00$", System.Globalization.NumberStyles.Currency | System.Globalization.NumberStyles.Number);
// Output is 2

So my problem is that why both are giving same output as I have changed their separators.

No correct solution

OTHER TIPS

I have to agree with Jon in that the formatting made it hard to understand. First of all your second line will not result in the value 2 and your have to rewrite it to:

var result = decimal.Parse("200", NumberStyles.Currency);

This will result in the value 200 (not 2!). The culture-info-acrobatic you are doing in the middle part could be shortened to

var result = decimal.Parse("2.00$", NumberStyles.Currency, CultureInfo.GetCultureInfo("en-US"));
Console.WriteLine(result);
// output on console is 2

This overload of the Parse-method will take the culture you want to use for conversion as the 3rd parameter.

If you put in another culture like fr-CA there will be thrown a FormatException. So this is correct and at this point I'm a confused on what you want to do now? Do you want to create your own culture using the strings contained in the input? Thats makes no sense to me. Maybe your want to check if a input-strings matches the culture resp. is valid in a culture? Then you should take a deeper look to TryParse.

Just another hint: Be careful not to mix parsing with output formatting! Changing the culture for the complete thread was not very useful in this scenrio too :-).

I think this snippet can put you in a right way.

CultureInfo cultureCA = CultureInfo.CreateSpecificCulture("fr-CA");
NumberFormatInfo numberFormat = cultureCA.NumberFormat;

numberFormat.CurrencyGroupSeparator = ".";
numberFormat.CurrencyDecimalSeparator = ",";
Thread.CurrentThread.CurrentCulture = cultureCA;
decimal num = Decimal.Parse("2,00$", System.Globalization.NumberStyles.Currency 
                    | System.Globalization.NumberStyles.Number);

Console.WriteLine(num == 2M);
// true, num is 2 

num = Decimal.Parse("2.00$", System.Globalization.NumberStyles.Currency 
                   | System.Globalization.NumberStyles.Number);
Console.WriteLine(num == 200M);
// true, num is 200 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top