Is it inappropriate to use InvariantCulture
? And, if used, would it affect any global settings?
Dim hh As Decimal = 123456789.123456
' Is this an appropriate usage?
Dim ll As String = hh.ToString("N4", CultureInfo.InvariantCulture)
The InvariantCulture property can be used to persist data in a culture-independent format. This provides a known format that does not change and that can be used to serialize and deserialize data across cultures.
The CultureInfo class provides culture-specific information, such as the language, sublanguage, country/region, calendar, and conventions associated with a particular culture. This class also provides access to culture-specific instances of the DateTimeFormatInfo, NumberFormatInfo, CompareInfo, and TextInfo objects.
A DateTimeFormatInfo that defines the culturally appropriate format of displaying dates and times.
Is using CultureInfo.InvariantCulture bad?
It entirely depends on what you're trying to achieve. If you're trying to format values to be machine-readable, it's almost always the right thing to do. If you're trying to format them for users, it may well be inappropriate.
Will it effect the global culture?
Specifying the invariant culture in a method call won't change the current thread's current culture, if that's what you're worried about.
Is using CultureInfo.InvariantCulture bad?
No. It exists for a reason. It allows you to parse text in a consistent, reliable manner, no matter what the current system culture happens to be.
However, it shouldn't be used (normally) to parse user input, but rather parsing text where you know it was written in the same manner that InvariantCulture expects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With