The recent article about globalizing in VB.NET demonstrates how a number can be formatted there, but what about VB 6?
Veteran VB 6'er (and frequent co-author) Peter Zilahy Ingerman (Getting Your System Out, Calculating a Contrasting Color Code, and Symbolic Logic) read my article and passed along a trick that works for him.
First, note that VB 6 can automatically format a number according to the "Regional Option" set in Windows fairly easily. For example, if I set my system to "Slovenian" I can format a number using the Format function in VB 6 ...
I then get
... in proper Slovenian form.
Peter said he often had a slightly different problem. "The issue arises in the context where ... I allow the user to *enter* numbers in a locale-specific way, so I have to parse them into my internal standard form." In other words, how do you figure out if the user entered, "123.456,78" or "123,456.78".
You can do this in VB 6 using API calls. The code,
Private Declare Function GetUserDefaultLCID% Lib "kernel32" ()
Private Sub Command1_Click()
Dim LCID As Long
'get the user's Locale ID
LCID = GetUserDefaultLCID()
... will return an LCID value of 1060 for Slovenian and 1033 for United States English. You can then plug this into the GetNumberFormat Win32 API and get a value in a NUMBERFMT structure that can be interrogated to determine what the local number formatting culture is. No less an authority than Dan Appleman states in his landmark book, A Guide to the Win32 API, "This structure is tricky to use from Visual Basic." (And he doesn't give an example.)
Or ... you can use a simple two line code snippet in VB 6 that Peter suggests.
LocaleDecimal = Mid$(CStr(11 / 10), 2, 1)
LocaleComma = Chr$(90 - Asc(LocaleDecimal))
The variables LocaleDecimal and LocaleComma will contain the appropriate symbol according to the Windows setting after this code executes. The key to making this happen is to get Windows to give you a string with the appropriate formatting. If you have the region set to Slovenian (or any other area where comma is the decimal symbol), CStr(10 / 11) returns the string "1,1". In my testing, CStr(1.1) does too, but Peter felt that a division avoids a "circular definition" in the code.
The second line works because the ASCII value of a comma plus a decimal equal 90 in any culture. So if you subtract one, you get the other.
Cultural Values are great!