Formatting Input And Output Strings For Multiple Countries - Keysight

When developing an application that will be used in multiple countries consider how numbers are represented in those countries. For example in the U.S. a large number can be punctuated with commas. A period is used in the U.S. to divide the integer and fractional part of a number. The same number in Germany would appear quite differently, a comma would be used as the decimal separator. Visual Basic has a locale feature to format numbers based on where the application is being used. This tutorial will focus on the radix of a number. For additional information, search in Visual Basic help for: Locale-Aware Functions, Writing International Code in Visual Basic, or International File Input/Output.

To understand the implications of Visual Basic locale feature, you must understand the terms system locale, code locale and instrument locale:

  • System locale is the locale of the user who runs the program. It is used as a reference for user input/output (i.e., TextBox on a form) and uses the Control Panel settings provided by the operating system (Start > Setting > Control Panel, and click Regional Options). If the system locale is German and the user enters the constant pi, the user would use a comma for a radix in a text box and enter the string "3,142". If the system locale is English (US) the user would enter "3.142" in the text box.
  • The code locale is always English (United States) in Visual Basic regardless of which international version you use. Numbers saved as a double are saved in the same way regardless of the system locale. Visual Basic uses a decimal point. The conversions from a number to a string and from a string to a number are dependent on the system locale and the Visual Basic method used.
  • Instrument locale is English (US) for Keysight Instruments and for most instruments that return strings. If pi is returned by such an instrument as a string, it would look like this: "3.142” Note that this is true only if the value of pi is returned as a string. If the value is returned as a number, the conversion is done correctly within the I/O command.

In the United States, the radix in the code locale is a decimal. Similarly, the instrument locale is usually a decimal. However in some system locales outside the US, the locale is a comma. Similar issues apply to dates and currency.

In Visual Basic there are functions that are locale aware and others that provide the same functionality that are not locale aware. If the code locale and the instrument locale are both the same, there are no problems. Most of the issues arise with string-to-number and number-to-string conversions when the system locale is different than the code locale. You may use the non-locale aware methods to avoid conversion mistakes in Visual Basic.

Locale aware methods: CStr, CDbl, CSng, Cint, CLng, CDate, CCur, Format(number)

Non-locale aware methods: Str, Str$, Val(string)

Let’s look at some examples to help clarify the issue.  Assume that a string with the value pi, "3.142" with the decimal as the radix (instrument locale is English).  You wish to put this value into a double called dblNumber.  If the system locale is German, then the code will incorrectly place into the variable the value 3142 because the decimal is treated as a thousands separator.  If the locale is French, then it will raise an error since the decimal is not recognized as a valid character.

Dim strTemp As String Dim dblNumber As Double strTemp = "3.1412" dblNumber = strTemp

This code will give the same incorrect result:

dblNumber = CDbl("3.142")

Instead, use the Val(string) method to convert a string to a double. This code will get the correct value.

dblNumber = Val("3.142")

With the system locale still in German, send the double value of pi to the instrument. You must use a non-locale aware function to send the string "3.142" with a decimal. The following locale aware functions will not work. They use a comma for a radix instead of the needed decimal. This creates the string "3,142":

strTemp = dblNumber strTemp = Format$(dblNumber)

Use the Str$(number) method to convert numbers to strings. This non-locale aware function will create the string "3.142" that can be sent to the instrument.

strTemp = Str$(dblNumber)

Suppose your instrument returned the number “3.142” and you want to display the result in a text box using system locale. First, convert the string to a number with the Val(string) command and convert it back to a string as locale aware.

TextBox.Text = Val("3.142")

Similarly, to send the correct value to the instrument that the user enters in a TextBox regardless of their system locale, use this code to convert the TextBox string to the code locale and then to an instrument locale string.

dblNumber = TextBox.Text strTemp = Str$(dblNumber)

In addition, using Formatted I/O can avoid some of these locale issues in VISA COM by sending and receiving numbers using the VISA COM commands. To convert a string number from the instrument to a double, use the ReadNumber command. VISA COM uses the same code and instrument locale (English) as Visual Basic and the instrument.

instrument.ReadNumber()

To send a number to an instrument, use the WriteNumber command. This works similarly to the ReadNumber command and uses the same code and instrument locale as Visual Basic and the instrument. The example below writes the double offset to a DMM. More information about the ReadNumber and WriteNumber commands can be found in the fourth topic of this tutorial Handling Data with Instruments.

instrument.WriteString "CALC:NULL:OFFS ", False instrument.WriteNumber offset, VisaComLib.IEEEASCIIType.ASCIIType_BSTR

Từ khóa » Visual Basic Double To Decimal