6

Is there a culture that doesn't use dashes (-) for negative numbers?

If I won't have to deal with negative numbers, would int.ToString() yield the same string of numbers in all cultures?

I have a piece of code with int.ToString(CultureInfo.InvariantCulture) and I want to make sure I can simplify it and remove the InvariantCulture parameter.

Manuel
  • 10,869
  • 14
  • 55
  • 86
  • Even without negatives, the number `1234` can be represented by different cultures as `1,234`, `1 234` or `1234`, for example. – Pierre-Luc Pineault Feb 27 '15 at 19:29
  • Periods can also be used as thousands separator. And decimals can use periods or commas: `1,234.5` vs `1.234,5` – Lucas Feb 27 '20 at 20:11
  • *"I have a piece of code with int.ToString(CultureInfo.InvariantCulture) and I want to make sure I can simplify it and remove the InvariantCulture parameter."* Why?? – ToolmakerSteve Aug 27 '20 at 22:20

2 Answers2

6

You shouldn't only consider cultures, but also human beings that set their Windows regional settings to have a weird negative sign, or other parameters.

Yes, you can actually set those:

Windows 8.1 regional settings dialog

I guess there is (almost) no one setting this, but better safe than sorry. Also, I won't assume you don't have to deal with negatives. Some day you will, and then this code will still work. Robust coding is better than assuming.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Also related is this [question](http://stackoverflow.com/questions/8492449/is-int32-tostring-culture-specific) and the corresponding answer – Neil Mountford Feb 27 '15 at 19:27
  • Thx. What if I only have to deal with positive numbers? Would it be safe then? I can't imagine that it wouldn't... – Manuel Feb 27 '15 at 19:28
  • Me too, but *what if*. And what if the logic changes some day so negatives *are* allowed? You have the chance to do it right now. – Patrick Hofman Feb 27 '15 at 19:29
0

There are cultures where it will be put in a brackets. Also, depending whether in the culture you write from left to right or from right to left the dash can be on a different side of the number. So with negative numbers you are looking into at least three possible formattings

  • -123
  • 123-
  • (123)

Take a look at MSDN for more details.

Also even if you are not using negative numbers your result still can be affected by decimal separator. Depending on the culture it can be a coma , or a dot .. So 123456789 can have two forms:

  • 123.456.789
  • 123,456,789
PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68
  • The above might not be what happens *by default*. Doc says that the default is to use `G` format. [G format doc](https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#GFormatString) says: "*The result string is affected by ... NumberFormatInfo properties ... NegativeSign, NumberDecimalSeparator, PositiveSign (of exponent). For an Integer, only the first (NegativeSign) is relevant. No mention of brackets, or of digit-group specifiers. Unclear whether left-to-right applies at this step, or at a later step when composing a line of text... – ToolmakerSteve Aug 27 '20 at 22:16