1. Home
  2. Computing & Technology
  3. Visual Basic

Getting Back to VB.NET, however ...

By Dan Mabbutt, About.com

Let's take an example of an amount that you need to display. Say the actual value you want to display is 12,345.67. Right away, I'm guilty of culture bias because I used a period between the whole number part and the decimal part of the number and a comma between the third and fourth digit in the whole number. Not all cultures do that. Many reverse the comma and the period. In addition, the currency symbol will be different for every currency in the world.

In this article, I'm going to focus on displaying numbers in different cultures. For this particular aspect of globalization, the first object to learn about is the NumberFormatInfo class, one of the classes in System.Globalization.

Microsoft publishes sample code to get you started on this too, but their examples seem to me to be unnecessarily complicated. If Microsoft were to publish directions for crossing the street to the west, they would tell you how to travel around the world to the east but they still wouldn't tell you how to cross the street. For example, the Microsoft code at CultureInfo.CurrentCulture Property doesn't tell you how to actually format anything. The sample code here just crosses the street. That is to say, it's no more complicated than it absolutely has to be and it does explain how to format a number.

One of the first things to understand is that culture information is maintained by the current Windows thread in the value of Thread.CurrentCulture. So to check for the culture in a given Windows environment, use this code ...

Imports System.Threading
Public Class Form1
   Private Sub Button1_Click( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs) _
      Handles Button1.Click
      MessageBox.Show( _
         Thread.CurrentThread.CurrentCulture.ToString)
   End Sub
End Class

On my computer, this gives a result of "en-US". If I change the value to Slovenian in Windows XP, (Start > Control Panel > Date, Time, Language and Regional Options > Regional and Language Options, the program reports "sl-SI" instead.

To set the value of CurrentCulture to a given culture using VB.NET, just assign the correct value. Technically, this creates a new, neutral object and then assigns "Slovenian" to it. Since this only affects the current thread, the culture settings of the computer aren't changed.

Public Class Form1    Private Sub Button1_Click( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs) _
      Handles Button1.Click
      Thread.CurrentThread.CurrentCulture = _
         New CultureInfo("sl-SI", False)
   End Sub
End Class

A list of the CultureInfo values can be found at This Microsoft site. The "False" option determines whether this CultureInfo assignment uses the user-selected culture settings. Since earlier versions of Windows can have incorrect values, for example for the Euro currency symbol, using "False" will ensure that the .NET Framework value is used.

To format a number as Currency ("C") using the current CultureInfo information, use a "C" format character. For example,

MessageBox.Show(myCurrency.ToString("C"))

To download a complete Visual Basic Express 2005 program demonstrating the ideas in this article, Click Here.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. Globalization in Visual Basic .NET

©2009 About.com, a part of The New York Times Company.

All rights reserved.