Plunging into .NET Development

Weblog Pieter Gheysens
Microsoft .NET Development - C# - Enterprise Library - Visual Studio 2005 Team System - Compuware DevPartner - ...

Thursday, September 15

Formatting : Zero / Digit placeholder

0 and # are format characters to create custom numeric format strings.

Example :

         double myDouble = 33.945;


         Console.WriteLine(myDouble.ToString("#.0000")); //Output = 33.9450

         Console.WriteLine(myDouble.ToString("#.####")); //Output = 33.945


         Console.WriteLine(myDouble.ToString("000")); //Output = 034

         Console.WriteLine(myDouble.ToString("###")); //Output = 34

The overloaded ToString-method converts the double to its equivalent string representation, using the specified format.
  • Zero placeholder [0]
    In the first example the zero placeholders after the comma make sure that there are 4 digits printed in the output. Example 3 shows padding at the other side.
  • Digit placeholder [#]
    This specifier never displays the '0' character if no significant digit is in place (no padding). See example 2 and 4.
You can also use the format string to display other objects (for example a DateTime).

         DateTime dt = DateTime.Now;

         Console.WriteLine(dt.ToString("dddd d MMMM yyyy"));

Interesting links for standard/custom formatting :


Post a Comment

Links to this post:

Create a Link

<< Home