Reputation: 8271
I'm converting a routine from VB6 (a language I do not know) to C#. In the VB6 code it uses GetLocaleInfo() and SetLocaleInfo() and I posted a question to ask how to do this in C# and I was told to use "CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator" instead.
For my purposes I'm only interested in getting the decimal separator and setting it for whatever my current locale is. Following the links in the answers to the original question it wasn't obvious how to do this.
In the original code the programmer uses the American "." format internally for lots of string formatting and building so he first gets the current locale's separator (say, ",") and saves it, then he sets the current locale to use "." and does all his stuff, then he sets it back to what he saved when he finishes. My marching orders are to stick as closely as possible to the original design, which is why I asked about GetLocaleInfo() and SetLocaleInfo(), but if someone could show how to do this CultureInfo I'd appreciate it.
EDIT: I don't understand what people don't like about this question. I also don't understand what code people are looking for. This is all the code there is in the VB6
LCID = GetThreadLocale
rc = SetLocaleInfo(LCID, LOCALE_SDECIMAL, ".")
That is the code. I want to do the same thing in C#. My understanding from our VB6 programmer is that it gets the current locale setting (say, German) and says regardless of the fact that German uses "," as a decimal separator, we are going to use ".".
Upvotes: 2
Views: 13604
Reputation: 68
You appear to be looking for the "NumberDecimalSeparator" property of the "NumberFormatInfo" object (System.Globalization), as you mentioned. Here's the msdn article on it. The usage (from the msdn page) is as follows:
// Gets a NumberFormatInfo associated with the en-US culture.
NumberFormatInfo nfi = new CultureInfo( "en-US", false ).NumberFormat;
// Displays a value with the default separator (".").
Int64 myInt = 123456789;
Console.WriteLine( myInt.ToString( "N", nfi ) );
// Displays the same value with a blank as the separator.
nfi.NumberDecimalSeparator = " ";
Console.WriteLine( myInt.ToString( "N", nfi ) );
You can change the separator with the NumberDecimalSeparator = "" line as seen above. All of your numeric outputs will use the separator as specified.
Likewise, you can use the NumberGroupSeparator property to handle the separation of whole number digits:
nfi.NumberGroupSeparator = ".";
...will make "123,456,789" display as "123.456.789", etc.
Hopefully that is a more clear and concise explanation than the one you linked.
Upvotes: 3