Visual Basic Locale/Regionalization Routines

GetLocaleInfo: Regional Locale Numeric Settings
     
Posted:   Monday March 20, 2000
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows NT4
OS restrictions:   None
Author:   VBnet - Randy Birch
     

Related:  

GetLocaleInfo: Regional Locale Country Settings
GetLocaleInfo: Regional Locale Currency Settings
GetLocaleInfo: Regional Locale Date Settings
GetLocaleInfo: Regional Locale Numeric Settings
GetLocaleInfo: System Calendar Information
     
 Prerequisites
VB5 or VB6.

National language support functions provide several values that can be used to retrieve the user's international locale information regarding representation of numeric data.

This page shows how to retrieve this information simply into text boxes. Although it may not be clear from the illustration, the decimal separator returned on my system is a period, the group separator a comma, and the negative sign a minus.

Any required information from this exercise could be easily wrapped into a function or class.

 BAS Module Code
Place the following code into the general declarations area of a bas module:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Const LOCALE_SDECIMAL              As Long = &HE     'decimal separator
Public Const LOCALE_STHOUSAND             As Long = &HF     'thousand separator
Public Const LOCALE_SGROUPING             As Long = &H10    'digit grouping
Public Const LOCALE_IDIGITS               As Long = &H11    'number of fractional digits
Public Const LOCALE_ILZERO                As Long = &H12    'leading zeros for decimal
Public Const LOCALE_INEGNUMBER            As Long = &H1010  'negative number mode
Public Const LOCALE_SNATIVEDIGITS         As Long = &H13    'native ASCII 0-9
Public Const LOCALE_SPOSITIVESIGN         As Long = &H50    'positive sign
Public Const LOCALE_SNEGATIVESIGN         As Long = &H51    'negative sign

Public Declare Function GetThreadLocale Lib "kernel32" () As Long

Public Declare Function GetSystemDefaultLCID Lib "kernel32" () As Long

Public Declare Function GetLocaleInfo Lib "kernel32" _
   Alias "GetLocaleInfoA" _
  (ByVal Locale As Long, _
   ByVal LCType As Long, _
   ByVal lpLCData As String, _
   ByVal cchData As Long) As Long


Public Function GetUserLocaleInfo(ByVal dwLocaleID As Long, ByVal dwLCType As Long) As String

   Dim sReturn As String
   Dim r As Long

  'call the function passing the Locale type
  'variable to retrieve the required size of
  'the string buffer needed
   r = GetLocaleInfo(dwLocaleID, dwLCType, sReturn, Len(sReturn))
    
  'if successful..
   If r Then
    
     'pad the buffer with spaces
      sReturn = Space$(r)
       
     'and call again passing the buffer
      r = GetLocaleInfo(dwLocaleID, dwLCType, sReturn, Len(sReturn))
     
     'if successful (r > 0)
      If r Then
      
        'r holds the size of the string
        'including the terminating null
         GetUserLocaleInfo = Left$(sReturn, r - 1)
      
      End If
   
   End If
    
End Function
 Form Code
Create a form containing nine textboxes named Text1 through Text9 (not in a control array). Add labels as desired, and a command button (Command1). Add the following code to the form:

Option Explicit


Private Sub Command1_Click()

   Dim LCID As Long
   
   LCID = GetSystemDefaultLCID()

  'LOCALE_SDECIMAL
  'Character(s) used as the decimal separator.
  'The maximum allowed is four.
   Text1.Text = GetUserLocaleInfo(LCID, LOCALE_SDECIMAL)
   
  'LOCALE_STHOUSAND
  'Character(s) used to separate groups of digits to
  'the left of the decimal. The maximum allowed is four.
   Text2.Text = GetUserLocaleInfo(LCID, LOCALE_STHOUSAND)

  'LOCALE_SGROUPING
  'Sizes for each group of digits to the left of the
  'decimal. An explicit size is needed for each group,
  'and sizes are separated by semicolons. If the last
  'value is zero, the preceding value is repeated. For
  'example, to group thousands, specify 3;0.
  'Indic locales group the first thousand and then group
  'by hundreds - for example 12,34,56,789, which is
  'represented by 3;2;0.
   Text3.Text = GetUserLocaleInfo(LCID, LOCALE_SGROUPING)
   
  'LOCALE_IDIGITS
  'Number of fractional digits. The maximum allowed is two.
   Text4.Text = GetUserLocaleInfo(LCID, LOCALE_IDIGITS)
   
  'LOCALE_ILZERO
  'Specifier for leading zeros in decimal fields. The maximum
  'allowed is two. The specifier can be one of the following values:
   Select Case GetUserLocaleInfo(LCID, LOCALE_ILZERO)
      Case "0": Text5.Text = "0 - No leading zeros."
      Case "1": Text5.Text = "1 - Has leading zeros."
   End Select
     
  'LOCALE_INEGNUMBER
  'Negative number mode, that is, the format for a negative
  'number. The maximum allowed is two. The mode can be
  'one of these values:
   Select Case GetUserLocaleInfo(LCID, LOCALE_INEGNUMBER)
      Case "0": Text6.Text = "0 - Left parenthesis, number, right parenthesis, ie (1.1)"
      Case "1": Text6.Text = "1 - Negative sign, number, ie -1.1"
      Case "2": Text6.Text = "2 - Negative sign, space, number, ie - 1.1"
      Case "3": Text6.Text = "3 - Number, negative sign, ie 1.1-"
      Case "4": Text6.Text = "4 - Number, space, negative sign, ie 1.1 -"
   End Select
   
  'LOCALE_SNATIVEDIGITS
  'Native equivalents to ASCII zero through 9.
   Text7.Text = GetUserLocaleInfo(LCID, LOCALE_SNATIVEDIGITS)

  'LOCALE_SNEGATIVESIGN
  'String value for the negative sign.
  'The maximum allowed is five.
   Text8.Text = GetUserLocaleInfo(LCID, LOCALE_SNEGATIVESIGN)

  'LOCALE_SPOSITIVESIGN
  'String value for the positive sign.
  'The maximum allowed is five.
   Text9.Text = GetUserLocaleInfo(LCID, LOCALE_SPOSITIVESIGN)

End Sub
 Comments
Save the program and run. The values displayed should correspond to the Codepage and Regional Settings for your system.
While the GetSystemDefaultLCID function retrieves the system default locale identifier, this is often inappropriate or insufficient in a networked environment or under an operating system where multiple locales have been installed. For example, it is possible for a network admin rolling out a standard image to have the user's default locale set to one differing from the base OS installation, and thus the system default locale.

In this situation Windows' provides an alternate API you can use to obtain the LCID for the current user ... GetUserDefaultLCID. Defined identically to GetSystemDefaultLCID, GetUserDefaultLCID function retrieves the user default–locale identifier and is therefore the most appropriate API to use when it is the user's locale you are interested in, rather than that of the system.


 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter