Click here to Skip to main content
Click here to Skip to main content
Go to top

Culture Specific Number Formatting in ASP.NET

, 17 Jun 2008
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

yordan_georgiev
Web Developer Oxit Oy
Finland Finland
I work in OXIT - a small IT consulting company, which has participated in the building of the most sophisticated IT Systems for several big Finnish and international companies (including Fortune 500 members) and continues to provide highly sophisticated IT Solutions to its customers.
 
I enjoy designing and implementing software or small scripts in different programming languages.
 
I am fascinated by the magic of software, which has the power to change the world.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 17 Jun 2008
Article Copyright 2008 by yordan_georgiev
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid