Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Culture Specific Number Formatting in ASP.NET

0.00/5 (No votes)
17 Jun 2008 1  
Presents the basics of applying custom number formatting.

Introduction

Number and date formatting are issues consuming time and effort, while things should not be so complicated ...

Background

These code snippets are slightly modified versions of the code snippets found on MSDN. One learns better by doing instead of studying long explanations and theory explanations ... especially when the activity in question is coding.

Using the Code

Just create a new project, copy paste the whole code, run it with F5, and try to change some of the parameters. It is important to realize that there are different sets of cultures (usually one for a country) which define the formatting of numbers and dates; however, C# allows the developer to override some of the specific formatting in a chosen culture.

// This code example demonstrates the String.Format() method.
// Formatting for this example uses the "fi-FI" culture.

using System;
using System.Globalization;

class Sample
{
    enum Color { Yellow = 1, Blue, Green };
    static DateTime thisDate = DateTime.Now;

    public static void Main()
    {
        string s = "";
        Console.Clear();

        //Create first the format provider the String.Format 
        //will use to format our string
        CultureInfo cultureToUse = new CultureInfo("fi-FI");
        Console.WriteLine("Using the following CultureInfor " + 
                          cultureToUse.Name);

        //Now practice some decimal numbers 
        //Here we override the culture specific formattings
        cultureToUse.NumberFormat.CurrencyDecimalDigits = 3;
        cultureToUse.NumberFormat.NumberDecimalDigits = 3;
        cultureToUse.NumberFormat.NumberGroupSeparator = " ";
        cultureToUse.NumberFormat.CurrencySymbol = "euro";
        cultureToUse.NumberFormat.NumberDecimalSeparator = ",";
        
        // Format a negative integer or floating-point number in various ways.
        Console.WriteLine("Standard Numeric Format Specifiers");
        s = String.Format( cultureToUse , 
            "(C) Currency: . . . . . . . . {0:C}\n" +
            "(D) Decimal:. . . . . . . . . {0:D}\n" +
            "(E) Scientific: . . . . . . . {1:E}\n" +
            "(F) Fixed point:. . . . . . . {1:F}\n" +
            "(F) Floating point:. . . . . .{2:F5}\n" +
            "(G) General:. . . . . . . . . {0:G}\n" +
            "    (default):. . . . . . . . {0} (default = 'G')\n" +
            "(N) Number: . . . . . . . . . {0:N2}\n" +
            "(P) Percent:. . . . . . . . . {1:P}\n" +
            "(R) Round-trip: . . . . . . . {1:R}\n" +
            "(X) Hexadecimal:. . . . . . . {0:X}\n",
            123456789 ,  -123.45f , 123456789.123456789);
        Console.WriteLine(s);


        Console.WriteLine("\n Using as an input a string");
        double aDouble = System.Convert.ToDouble("123456789,123456789");

        s = String.Format(cultureToUse,
            "{0:N3}" , aDouble);

            //"(F) Fixed point:. . . . . . . {0:F}\n" +
            //"(F) Floating point:. . . . . .{0:F3}\n" +
            

        //    122 , aDouble);
        Console.WriteLine(s);

        s = String.Format(cultureToUse,
        //    "(D) Decimal:. . . . . . . . . {0:D}\n" +
        //    "(F) Fixed point:. . . . . . . {0:F5}\n" +
            "(N) Number: . . . . . . . . . {0:N5}\n" +
        //  "(F) Fixed point:. . . . . . . {0:F}\n" +
        //    "(F) Floating point:. . . . . . . {0:F3}\n" +
            123456789.123456789, 11111111111111111.123456789);
        Console.WriteLine(s);

        double d = 123456789.2345678901234567890;
        
        Console.WriteLine("Floating-Point:\t{0:F16}", d);  // 1.2345678901234600
        Console.WriteLine("When using the toString Methods" + 
                          d.ToString("{0:F16}", cultureToUse));
        
        // Format the current date in various ways.
        Console.WriteLine("Standard DateTime Format Specifiers");
        s = String.Format(cultureToUse , 
            "(d) Short date: . . . . . . . {0:d}\n" +
            "(D) Long date:. . . . . . . . {0:D}\n" +
            "(t) Short time: . . . . . . . {0:t}\n" +
            "(T) Long time:. . . . . . . . {0:T}\n" +
            "(f) Full date/short time: . . {0:f}\n" +
            "(F) Full date/long time:. . . {0:F}\n" +
            "(g) General date/short time:. {0:g}\n" +
            "(G) General date/long time: . {0:G}\n" +
            "    (default):. . . . . . . . {0} (default = 'G')\n" +
            "(M) Month:. . . . . . . . . . {0:M}\n" +
            "(R) RFC1123:. . . . . . . . . {0:R}\n" +
            "(s) Sortable: . . . . . . . . {0:s}\n" +
            "(u) Universal sortable: . . . {0:u} (invariant)\n" +
            "(U) Universal sortable: . . . {0:U}\n" +
            "(Y) Year: . . . . . . . . . . {0:Y}\n",
            thisDate);
        Console.WriteLine(s);

        // Format a Color enumeration value in various ways.
        Console.WriteLine("Standard Enumeration Format Specifiers");
        s = String.Format(cultureToUse , 
            "(G) General:. . . . . . . . . {0:G}\n" +
            "    (default):. . . . . . . . {0} (default = 'G')\n" +
            "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
            "(D) Decimal number: . . . . . {0:D}\n" +
            "(X) Hexadecimal:. . . . . . . {0:X}\n",
            Color.Green);
        Console.WriteLine(s);
        System.Threading.Thread.Sleep(15000);
    }
}

Bonus Code

This implementation is a static method for a nice Finnish number formatting (well, you could change your culture since you now know how) and a couple of static methods for date conversion.

Just create a sample DateConverter class, copy paste the methods in it, and use them as DateConverter.FromObjToLongDateString( valueObject );.

public static string FormatDecimalNumber  ( bool flagNeedsFormatting , 
       ref string filledValue ) 
{

    //E.G. pass CultureInfo specifig string 
    if (flagNeedsFormatting == false)
        return filledValue;
    else if (filledValue !=null && flagNeedsFormatting &&
        filledValue.Equals ( String.Empty ) == false && 
        filledValue.Equals ( " " ) == false)
    {
        CultureInfo cultureToUse = new CultureInfo ( "fi-FI" );
        cultureToUse.NumberFormat.CurrencyDecimalDigits = 3;
        cultureToUse.NumberFormat.NumberDecimalDigits = 3;
        cultureToUse.NumberFormat.NumberGroupSeparator = " ";

        double doubleFilledValue = System.Convert.ToDouble ( filledValue );
        filledValue = String.Format ( cultureToUse , "{0:N3}" , 
                                      doubleFilledValue );
        return filledValue;
    } //eof if needs Formatting
    else
        return filledValue;

} //eof method FormatDecimalNumber 

public static string FromObjToShortDateString ( object filledValue )
{
    string strTime =  System.Convert.ToString ( filledValue ).Trim ();
    CultureInfo    culture = CultureInfo.CreateSpecificCulture ( "fi-FI" );
    DateTime dtDate = DateTime.Parse ( strTime , culture );
    return dtDate.ToShortDateString ();

} //eof method FromObjToShortDateString ( object o )

public static string FromObjToLongDateString ( object filledValue )
{
    string strTime =  System.Convert.ToString ( filledValue ).Trim ();
    //change fi-FI Finland to de-DE for German etc. 
    CultureInfo    culture = CultureInfo.CreateSpecificCulture ( "fi-FI" );
    DateTime dtDate = DateTime.Parse ( strTime , culture );
    return  dtDate.ToLongTimeString ();

} //eof method  FromObjToLongDateString ( object filledValue )

Points of Interest

Here is a how-to for creating truly global web-based applications. There should be a UserSettings table in the database, storing the specific culture info for each user, but for better usability, users should be given the choice to change individual culture specific meta characters, such as a decimal separators, group separators etc.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here