String formatting in WPF and Silverlight

If you’re building controls or applications that display numbers and dates, you’re going to want to have a good understanding of how to format them nicely. In WPF and Silverlight, this is achieved using the static String.Format method which has a few overloads. The method overload we’ll be looking at takes in a string and an object as the parameters. The string parameter is a format string which will be used to format the given object. The object is whatever we want to convert to a string such as a double or a DateTime. In this blog post we will look at just a few of the many format strings provided by WPF and Silverlight for formating doubles and DateTimes. The general structure of a format string is: “{0:*}” where the * is replaced with the format string itself.

C# String format for double

Most format strings for double values are comprised of some numbers or symbols followed by a decimal point followed by some more numbers or symbols. The most common string format for formatting a double value is to specify the number of decimal places. As seen in the examples below, the number of following zeros define the exact number of decimal places to display. The number of “#” symbols can be used to define a maximum number of decimal places while only displaying digits if they are not zero. The last example below shows how “0.0#” can be used to specify that at least one decimal place is always shown, but only a maximum of two decimal places.

// exactly two decimal places
String.Format("{0:0.00}", 123.4567);      // "123.46"
String.Format("{0:0.00}", 123.4);         // "123.40"
String.Format("{0:0.00}", 123.0);         // "123.00"
// at most two decimal places
String.Format("{0:0.##}", 123.4567);      // "123.46"
String.Format("{0:0.##}", 123.4);         // "123.4"
String.Format("{0:0.##}", 123.0);         // "123"
// one or two decimal places
String.Format("{0:0.0#}", 123.4567);      // "123.46"
String.Format("{0:0.0#}", 123.4);         // "123.4"
String.Format("{0:0.0#}", 123.0);         // "123.0"

C# String format for DateTime

Format strings for DateTime objects are built up as a sequence of grouped letters. Each letter targets a particular part of the DateTime such as the year, day or hour. The number of letters in each group defines how that part should be formatted. Below is a list of some of the letters and the results of applying them to a DateTime.

DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 0);
String.Format("{0:y yy yyy yyyy}", dt);  // "8 08 008 2008"      year
String.Format("{0:M MM MMM MMMM}", dt);  // "3 03 Mar March"     month
String.Format("{0:d dd ddd dddd}", dt);  // "9 09 Sun Sunday"    day
String.Format("{0:h hh H HH}",     dt);  // "4 04 16 16"         hour 12/24
String.Format("{0:m mm}",          dt);  // "5 05"               minute
String.Format("{0:s ss}",          dt);  // "7 07"               second
String.Format("{0:t tt}",          dt);  // "P PM"               AM or PM

By using any combination of these letter groups within a format string, you can display a DateTime in any possible way you need. Between each letter group, you may also want to include a symbol such as a comma, colon or slash. The slash and colon symbols are special characters known as the date separator and time separator respectively. One thing to keep in mind is that the date and time separator characters may be displayed differently depending on the current culture of the application. Month and day names will also be displayed in different languages based on the culture.

// DateTime format examples
String.Format("{0:MM/dd/yy}", dt);            // "03/09/08"
String.Format("{0:dddd, MMMM d, yyyy}", dt);  // "Sunday, March 9, 2008"
String.Format("{0:d/M/yyyy HH:mm:ss}", dt);   // "9/3/2008 16:05:07"

Format strings in XAML

If you ever come across a situation where you need to set a property of an object in XAML to be a format string, you may notice a small problem. Format strings such as the ones described in this blog post start and end with the curly bracket symbols. So the following code will fail to compile because the curly bracket is a special character which XAML will interpret in its own way rather than using it as a string.

<MyFormattingObject FormatString="{0:0.0}" />

To get around this issue, all we need to do is place an empty pair of curly brackets before the format string to tell XAML to interpret it as a string value.

<MyFormattingObject FormatString="{}{0:0.0}" />

More information

For a more extensive list of format string tips and tricks, you can follow this link to find DateTime formats, and check here to find string formats for doubles.

Tagged as General, Silverlight, WPF

Leave a Reply


Join our mailer

You should join our newsletter! Sent monthly:

Back to Top