Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String to Numeric conversion and group separator

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

like image 816
Loic Avatar asked Nov 06 '22 15:11

Loic


1 Answers

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).

like image 130
Marc Gravell Avatar answered Nov 14 '22 21:11

Marc Gravell