13

Resharper is warning me that I need to specify a string culture when doing an int.ToString()

For example:

int Value = Id.ToString(); // Where Id is an int

Is this just resharper being pedantic, or is there a reason I need to give a culture setting for converting an int to a string?

And if there is a reason to do this - what is the best to use, when my site is used around the globe? Should it just reflect the server settings, so that the internal conversion is done safely?

Craig
  • 18,074
  • 38
  • 147
  • 248

2 Answers2

17

Because, for example, English cultures use '.' as a decimal point, where as French cultures use ',' and the opposite to separate thousands. Specifying a culture removes this ambiguity so that code executes identically on Windows operating systems configured for different languages, particularly if the parsed values are being committed to persistent storage.

Martin Costello
  • 9,672
  • 5
  • 60
  • 72
  • 1
    But doesn't .NET base it off of the current thread culture info? So that way it will serialiaze it correctly for the the user who is using it? My point being that users want to see it in their culture, not in some other culture that they won't understand, so why would Resharper suggest this? – Dan Drews Apr 05 '14 at 22:49
  • 4
    It does, but ReSharper (and FXCop on which it is based) point this out as a "did you really mean to do this" kind of thing. It's better to be explicit and use CultureInfo.CurrentCulture if you truly want that behaviour. Otherwise use CultureInfo.InvariantCulture – Martin Costello Apr 05 '14 at 22:51
  • Ah, perfect! I forgot about the decimals. It my case, I'm taking a SQL identity column, INT, which I believe will always be a whole number, and converting it to a string for a UI dropdown box's value. Your answer explains where else this would be useful. I'm then guessing that as long as I use "CultureInfo.InvariantCulture", all will be uniform? – Craig Apr 05 '14 at 22:51
  • That's correct. "Invariant" means unchanging, so will always produce the same result for a given input regardless of the OS locale. – Martin Costello Apr 05 '14 at 22:55
  • The default `ToString()` doesn't use thousands separator. – Jesse Good Apr 05 '14 at 22:56
  • Also thousand separators will still apply even with INT. – Martin Costello Apr 05 '14 at 22:56
  • 1
    @JesseGood: That's right, but it does get the character to specify a negative value from the culture. – Guffa Apr 05 '14 at 22:59
7

The reason is that the string representation of a number vary from country to country.

Some use a '.' as the thousand separator, some use ',', some just doesn't use any symbol at all.

The ToString overload with no parameter uses the current culture so your output will vary depending on the computer's settings.

That said, my rule of thumb is:

  • every internal representation must be InvariantCulture (never relay on server settings!)
  • everything on the presentation must use end user's locale

Hope it helps

Manuel Spezzani
  • 1,041
  • 7
  • 15