Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do any .NET cultures handle canonical representations of integers differently?

Tags:

c#

.net

parsing

The canonical representation of an integer is as a sequence of ASCII decimal digit characters, with a prefixed '-' only if the number is negative. No initial '0' except when the number being represented is zero. No other characters, including commas, currency symbols, spaces, units, '+', decimal points, etc.

Are the any .NET cultures such that int.TryParse will return a different value to the invariant culture when given a string containing a canonical representation of an integer?

If there are none, does .NET guarantee that any culture that may be added in the future will continue to behave in this way?

Or to put it another way... Do I need to specify the invariant culture when calling int.TryParse if I only care about this variety of input?

like image 695
billpg Avatar asked Oct 21 '22 14:10

billpg


1 Answers

Even the negative sign has different options depending on the culture. If you do a

var numbers = from c in CultureInfo.GetCultures(CultureTypes.AllCultures)
              group c by c.NumberFormat.NumberNegativePattern into g
              select g;

and then examine numbers you will see that some cultures use a different option for the negative sign, for example the culture lo or lo-LA.


As a rule of thumb I use CultureInfo.InvariantCulture for all purposes except for displaying it to the user.

like image 146
Dirk Avatar answered Oct 23 '22 04:10

Dirk