views:

404

answers:

3

Hello everybody.

When I try to convert a string to a numeric value with Parse or TryParse or Convert.ChangeType, I can't manage de thousand separator as it is defined in the system:

if I enter :

var d = double.Parse("1,234", CultureInfo.CurrentUICulture);

it does not return 1234.

If I want to use the group separator, I must type :

var d = double.Parse("1,234", NumberStyles.Number, CultureInfo.CurrentUICulture);

This time, the result is that one expected.

But, I don't want to force the use of the thousand separator, I want use it only if the system specify it in the globalization settings. Is there a way to know if the separator is used (I know that I can read the group separator in CultureInfo.CurrentUICulture.NumberFormat.NumberGroupSeparator)

Cheers Loic

A: 

You could always use the second line you use

var d = double.Parse("1,234", NumberStyles.Number, CultureInfo.CurrentUICulture);

as it will correctly convert 1234 as well as 1,234 and it should cover 1.234 on systems that use '.' as the group separator

phsr
Yes, but in fact, I want to use the Convert.ChangeType function in order to make a generic string to numeric converter, and provide it as a string extension. But Convert.ChangeType has no overload with NumberStyles parameter. It just allows an IFormatProvider parameter.And by the way, what I don't understand is why the defaut setting for Parse does not meet my system international settings, where use of group separator is setted.
Loic
+1  A: 

Having Number (which includes AllowThousands) doesn't demand a comma - it simply allows it. So you could use Number with or without the comma. Or use Any or AllowThousands.

Note that "comma" is swappable with "thousands separator" - i.e. in some of Eurupe it may vary (period etc). If you mean "comma is thousands" then use a fixed culture (such as InvariantCulture).

Marc Gravell
I'm agree.But my problem is (assert that culture is 'fr-FR' and thousand separator is a space character), why :The following line works :double d = (double)Convert.ChangeType("1 234", typeof (double), _culture);While the following line throws a format exception ?int i = (int)Convert.ChangeType("1 234", typeof (int), _culture);
Loic
A: 

You should parse user input using CurrentCulture and not CurrentUICulture.

The property CurrentUICulture refers to the language on which the user interface is displayed and CurrentCulture to the current locale specified in Windows. This way a user working with an application that provides a user interface translated to english (CurrentUICulture) can still view/enter, for example, dates and numbers formatted as the locale that he set in Windows (CurrentCulture).

João Angelo
Hello, thank' s for your help.My application enables the user to change the language as run time (we are providing 7 differents languages). We changes the CurrentUICulture and the CurrentCulture when the user selects a language. We want the user view / enter dates and numbers as the selected language, and not as the locale set in the system.
Loic