CSharp - Format providers and CultureInfo

Introduction

CultureInfo returns a NumberFormatInfo or DateTimeFormatInfo object applicable to the culture's regional settings.

In the following example, we request a specific culture: english language in Great Britain:

This executes using the default NumberFormatInfo object applicable to the en-GB culture.

Demo

using System;
using System.Globalization;

class MainClass/* w w  w . j  a  v  a 2 s . c  o m*/
{
    public static void Main(string[] args)
    {
        CultureInfo uk = CultureInfo.GetCultureInfo("en-GB");
        Console.WriteLine(3.ToString("C", uk));
    }
}

Result

The next example formats a DateTime with invariant culture.

Invariant culture is always the same, regardless of the computer's settings:

Demo

using System;
using System.Globalization;

class MainClass/*from   w  w  w  . ja  va2 s  .com*/
{
    public static void Main(string[] args)
    {
        DateTime dt = new DateTime(2000, 1, 2);
        CultureInfo iv = CultureInfo.InvariantCulture;
        Console.WriteLine(dt.ToString(iv));            // 01/02/2000 00:00:00
        Console.WriteLine(dt.ToString("d", iv));       // 01/02/2000
    }
}

Result