.NET Power Tip 4: Playing with Format Strings


Many developers heavily underestimate the power of the format string. This post shows a couple of examples that can greatly facilitate a coders daily life.

Don’t know what a format string is, look here:

Another  great resource with some more advanced ideas:

Remember that there are multiple ways how a format string can be evaluated:

double d = 1.23456;

//Method 1: Calling string.Format()
string s1 = string.Format("{0:00.00}", d);   //01.23

//Method 2: Using the format string within the ToString() method:
//            Note that you can omit the curly bracket and obviously 
//            the position indicator. Does not work with all data types.
string s2 = d.ToString("00.00"); //01.23

//Method 3: Passing to a method that provides an overload that accepts
//            a format string
Console.WriteLine("{0:00.00}", d); //01.23

Here are some useful examples that I often use when coding:

Formatting String Values

string text = "hello";

//Pad string left side for a total of 10 characters
string s1 = string.Format("{0, 10}", text); //"-----hello" (- is the blank)

//Pad string right side for a total of 10 characters
string s2 = string.Format("{0, -10}", text); //"hello-----" (- is the blank)



Formatting Numeric Values

/Double Formatting: Display as Currency, Exponential, Percentage, Hex
    //Format as currency
    double d0 = 5784.434; //"Fr. 5 784.43" (Swiss Francs)
    string s0a = d0.ToString("C"); //5.7844E+003
    //Format as scientific number
    string s0b = d0.ToString("E4"); //5.7844E+003
    //Format as HEX
    int d0b = 342342790;
    string s0c = d0b.ToString("X"); //"1467BC86"
    //Format as percentage
    double d0a = 0.1258;
    string s0d = d0a.ToString("00.00%"); //"12.58%"

//Double Formatting: Rounding digits after decimal separator

    //round to one digit after separator
    double d = 1.23456;
    string s1a = string.Format("{0:0.0}", d);   //1.2
    //equal to:
    string s1b = d.ToString("0.0"); //1.2
    //round to three digits after separator
    string s2a = string.Format("{0:0.000}", d); //1.235
    //equal to:
    string s2b = d.ToString("0.000"); //1.235
    //NOTE: using '0' will fill the requested after decimal positions 
    //with zeroes if the number is shorter
    double d2 = 1.1;
    string s3a = string.Format("{0:0.000}", d2); //1.100
    //equal to:
    string s3b = d2.ToString("0.000"); //1.100
    //If you don't want the number to be filled with zeroes, use # instead
    double d3 = 1.1;
    string s4a = string.Format("{0:0.###}", d3); //1.1
    //equal to:
    string s4b = d3.ToString("0.###"); //1.1
//Double Formatting: Padding numbers before decimal separator with zeroes
    double d4 = 2.56;
    string s5 = string.Format("{0:00.000}", d4); //02.560
    string s6a = string.Format("{0:0000.00}", d4); //0002.56
    //equal to:
    string s6b = d4.ToString("0000.00"); //0002.56
//Adding thousand separators
    double d5 = 1222333444.66;
    string s7 = string.Format("{0:0,0.0}", d5); //1 222 333 444.0 
    //Note that on my system, blank is the thousand separator



Formatting DateTime

//Formatting a DateTime value for serialization or sorting 
//Fixed width and descendent order year-month-day-hours-minutes-seconds
    string s1 = string.Format("{0:s}", dt); //2013-09-22T21:27:49
    //UniversalTime (with Z indicator)
    string s2 = string.Format("{0:u}", dt); //2013-09-12 23:28:36Z
    //Formatting a date in accordance with RFC1123
    string s3 = string.Format("{0:r}", dt); //Tue, 22 Sep 2013 13:31:36 GMT


Of course all the approaches can be combined, for example to format output in table style:


DateTime dt = new DateTime(2010, 12, 01);
string name = "MSFT";
double d1 = 3.1423423324;
double d2 = 5.34222;

string text = string.Format("Data[{0:d}]:   {1, 8}     {3:000.00}   {2:C}", 
                            dt, name, d1, d2);

//Data[01.12.2010]:       MSFT     005.34   Fr. 3.14