Site Tools


How To Use GetLocale() and SetLocale() in VBScript

Developer: RhinoScript
Summary: How to format locale-sensitive numbers using GetLocale() and SetLocale().

Overview

Formatted numbers are locale-sensitive. That is, the way numbers are formatted differs depending on which region of the world you are in.

Here are a few examples:

CultureFormat
United States1,234,567.89
France1 234 567,89
Germany1.234.567,89
Switzerland1'234'567.89

More information

VBScript supports the functionality of retrieving and changing the current locale settings of your computer. The two functions that support this feature are GetLocale() and SetLocale(). GetLocale() returns the current locale on the client machine. SetLocale() sets the locale to the new specified locale. These functions can be used accordingly to localize numeric values.

The SetLocale() function requires one argument, the LCID, which is a short string, decimal value, or hex value that uniquely identifies a geographic locale. The various geographic locales are based upon the user's language, country, and culture. For example, the locale “English - United States” can be designated as either “en-us”, or “1033”, or “0x0409”.

This locale information is used to establish user preferences and formats for such things as alphabets, currency, dates, keyboard layout, and numbers. A list of these locales, and their return values, can be found here.

Note, if the LCID argument is set to zero, the locale will be set by the system.

Example

The following examples demonstrates the use of the SetLocale() function:

 Sub TestLocale()
 
   Dim dblValue : dblValue = 1234567.89
 
   Call SetLocale(1033) 'English - United States
   Rhino.Print "English - United States = " & FormatNumber(dblValue)
 
   Call SetLocale(1036) 'French - France
   Rhino.Print "French - France = " & FormatNumber(dblValue)
 
   Call SetLocale(1031) 'German - Germany
   Rhino.Print "German - Germany = " & FormatNumber(dblValue)
 
   Call SetLocale(2055) 'German - Switzerland
   Rhino.Print "German - Switzerland = " & FormatNumber(dblValue)
 
   Call SetLocale(0)
 
 End Sub
Output:
 English - United States = 1,234,567.89
 French - France = 1 234 567,89
 German - Germany = 1.234.567,89
 German - Switzerland = 1'234'567.89


developer/scriptsamples/getlocale.txt · Last modified: 2011/07/28 by dale