Formatting Numbers using IFormatProvider and ToString

by matt 10. January 2008 16:50

As I've had a number of people mention to me that they don't quite know how to use a class that implements the IFormatProvider interface.  In most case that come to my attention, we are specifically interested in date formats and number formats.

Below is the code for a simple console application in .Net 2.0 that will use the NumberFormatInfo class to format the display of a numeric value.  I've set up two instances of the NumberFormatInfo class, one for French formatting and one for British English formatting.  The CultureInfo class from the Globalization namespace has been used to load up the culture specific information that I need for this example.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace IFormatProvider
{
    class Program
    {
        static void Main(string[] args)
        {
            double testValue = 10.234;

            NumberFormatInfo britishFormatter = CultureInfo.CreateSpecificCulture("en-GB").NumberFormat;
            NumberFormatInfo frenchFormatter = CultureInfo.CreateSpecificCulture("fr-FR").NumberFormat;

            Console.WriteLine("In fr-FR, our value is {0}", testValue.ToString(frenchFormatter));
            Console.WriteLine("In en-GB, our value is {0}", testValue.ToString(britishFormatter));
            Console.ReadLine();
        }
    }
}

When you run the above code in Visual Studio, you will see the following output:

In fr-FR, our value is 10,234
In en-GB, our value is 10.234

This then got me thinking.  As I have now told the application what I want to do, I assume tha by default the ToString method on must have some kind of default based on the application thread.  I also know that the application thread has two sets of culture information; the UI culture and the system culture.  That led to the question; which is the default?

So I went ahead and wrote the following to see which culture information is the default.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Threading;

namespace IFormatProvider
{
    class Program
    {
        static void Main(string[] args)
        {
            double testValue = 10.234;

            Console.WriteLine(
                "Default Culture information: UI [{0}] - SYSTEM [{1}]",
                Thread.CurrentThread.CurrentUICulture.Name,
                Thread.CurrentThread.CurrentCulture.Name
                );
            Console.WriteLine(
                "Our Value: {0}",
                testValue.ToString()
                );

            Console.WriteLine("> Change CurrentUICulture to French...");
            Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("fr-FR");
            Console.WriteLine(
                "> Our Value: {0}",
                testValue.ToString()
                );

            Console.WriteLine("Change CurrentCulture to French...");
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
            Console.WriteLine(
                "> Our Value: {0}",
                testValue.ToString()
                );

            Console.ReadLine();
        }
    }
}

Interestingly, the output is as follows:

Our Value: 10.234
> Change CurrentUICulture to French...
> Our Value: 10.234
> Change CurrentCulture to French...
> Our Value: 10,234

So, for the sake of interest; it looks like CurrentCulture is used as the default.

References


Tags:

.Net Framework

Powered by BlogEngine.NET 1.5.0.7
Theme by Interakting

Interakting

A full service digital agency offering online strategy, design and usability, systems integration and online marketing services that deliver real business benefits and ensure your online objectives are met.

Calendar

<<  July 2010  >>
MoTuWeThFrSaSu
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar