Localization Like the Pros—Number formatting


Jump to: navigation, search
CSharp-Online.NET:Articles
C# Articles

Localize Like the Pros

© 2004 Wiley Publishing, Inc.

Number formatting

The number structures Int16, Int32, Int64, and so on, in the System namespace have an overloaded ToString() method. This method can be used to create a different representation of the number depending on the locale. For the Int32 structure, ToString() is overloaded with these four versions:

public string ToString();
public string ToString(IFormatProvider);
public string ToString(string);
public string ToString(string, IFormatProvider);

ToString() without arguments returns a string without format options. We can also pass a string and a class that implements IFormatProvider.

The string specifies the format of the representation. The format can be a standard numeric formatting string or a picture numeric formatting string. For standard numeric formatting, strings are predefined where C specifies the currency notation, D creates a decimal output, E scientific output, F fixed-point output, G general output, N number output, and X hexadecimal output. With a picture numeric format string, it is possible to specify the number of digits, section and group separators, percent notation, and so on. The picture numeric format string ###,### means two 3-digit blocks separated by a group separator.

The IFormatProvider interface is implemented by the NumberFormatInfo, DateTimeFormatInfo, and CultureInfo classes. This interface defines a single method GetFormat() that returns a format object.

NumberFormatInfo can be used to define custom formats for numbers. With the default constructor of NumberFormatInfo, a culture-independent or invariant object is created. Using the properties of NumberFormatInfo it is possible to change all the formatting options like a positive sign, a percent symbol, a number group separator, a currency symbol, and a lot more. A read-only culture-independent NumberFormatInfo object is returned from the static property InvariantInfo. A NumberFormatInfo object where the format values are based on the CultureInfo of the current thread is returned from the static property CurrentInfo.

To create the next example you can start with a simple Console Project. In this code, the first example shows a number displayed in the format of the culture of the thread (here: English-US, the setting of the operating system). The second example uses the ToString() method with the IFormatProvider argument. CultureInfo implements IFormatProvider, so create a CultureInfo object using the French culture. The third example changes the culture of the thread. Using the property CurrentCulture of the Thread instance, the culture is changed to German:

using System;
using System.Globalization;
using System.Threading;
 
namespace Wrox.ProCSharp.Localization
{
   class Class1
   {
      static void Main(string[] args)
      {
         int val = 1234567890;
 
         // culture of the current thread
         Console.WriteLine(val.ToString("N"));
 
         // use IFormatProvider
         Console.WriteLine(val.ToString("N", 
                           new CultureInfo("fr-FR")));
 
         // change the culture of the thread
         Thread.CurrentThread.CurrentCulture = 
 
                           new CultureInfo("de-DE");
         Console.WriteLine(val.ToString("N"));
      }
   }
}

The output is shown in Figure 17-4. You can compare the outputs with the previously listed differences for U.S. English, French, and German.


Image:557599-fg1704.jpg
Figure 17-4


Previous_Page_.gif Next_Page_.gif


Personal tools