Charlie Calvert on Elvenware

Writing Code and Prose on Computers

Elvenware

String Formatting Examples

Key Links

A call to string.Format consists of two parts. There are a set of format items which can be thought of as indexed placeholders. They look like this: {0}, {1}, etc. The second part is the list of parameters. The indexes in the format items refer to the index of the parameters. For instance {0} references the first parameter, and {1} references the second.

There are two types of string formatters. The standard formatters are like {0:C} to yield currency. The custom ones are like {0:00.00} to pad numbers with zeros or specify the number of decimals.

The standard formatters sometimes also accept a precision specifier to designate the number of Decimal digits: {0:C3} which yields values like $1.123 instead of $1.12.

Some Numbers:

double figure = 1.2340;
int million = 1000000;
int tenMillion = 10000000;
int simple = 1;

Some output:

Formatting double 1.234 with {0:C} to yield $1.23
Formatting double 1.234 with {0:G} to yield 1.234
Formatting double 1.234 with {0:0.0000} to yield 1.2340
Formatting Integer 1 with {0:D} to yield 1
Directory, Archive, Normal
Fixed Point {0:F}: 1.23
Formatting 1.234 with {0:E} to yield 1.234000E+000
Formatting 1000000 with {0:E} to yield 1.000000E+006
Formatting 10000000 with {0:E} to yield 1.000000E+007
Formatting 1.234 with {0:N} to yield 1.23
Formatting 1.234 with {0:N5} to yield 1.23400
Formatting 10000000 with {0:N} to yield 10,000,000.00
Formatting 10000000 with {0:N5} to yield 10,000,000.00000
Formatting double 1.234 with {0:P} to yeild 123.40 % (Percent)
Formatting double 1.234 with {0:R} to yield 1.234 (Round Trip)
Hexedecimal {0:X}: 989680
PadInteger: 01.00
PadInteger: 001
PadDouble: 01.23

How to achieve it:

using System.IO;

namespace StringsFormat
{
    public static class FormatExamples
    {
        #region Double(D)

        public static string FormatDouble01(double figure, out string formater)
        {
            formater = 
            	"Formatting double {0} with {{0:0.0000}} to yield {0:0.0000}";
            return string.Format(formater, figure);            
        }

        #endregion

        #region General(g)

        internal static string FormatGeneral01(double figure)
        {
            return string.Format(
            	@"Formatting double {0} with {{0:G}} to yield {0:G}", figure);
        }

        #endregion

        #region Currency(C)

        internal static string FormatCurrency01(double figure)
        {
            return string.Format(
            	@"Formatting double {0} with {{0:C}} to yield {0:C}", 
            	figure);
        }

        #endregion

        #region decimal(D)

        internal static string FormatDecimal01(int figure)
        {
            return string.Format(
            	@"Formatting Integer {0} with {{0:D}} to yield {0:D}", 
            	figure);
        }

        #endregion

        #region exponentional(E)

        internal static string FormatExponential01(double figure)
        {
            return string.Format(@"Formatting {0} with {{0:E}} to yield {0:E}", 
            figure);
        }

        #endregion

        #region fileAttributes(F)

        internal static string FormatFileAttributes()
        {
            FileAttributes f = 
            	FileAttributes.Archive | 
            	FileAttributes.Directory | 
            	FileAttributes.Normal;
            return f.ToString("F");
        }

        #endregion

        #region fixedPoint(F)

        internal static string FormatFixedPoint(double figure)
        {
            return string.Format(@"Fixed Point {{0:F}}: {0:F}", figure);            
        }

        #endregion

        #region numbers(n)

        internal static string FormatNumbers01(double figure)
        {
            return string.Format(@"Formatting {0} with {{0:N}} to yield {0:N}", 
            	figure);
        }

        internal static string FormatNumbers02(double figure)
        {
            return string.Format(@"Formatting {0} with {{0:N5}} to yield {0:N5}", 
            	figure);
        }

        #endregion

     
        #region roundTrip(P)

        internal static string FormatRoundTrip(double figure)
        {
            return string.Format(
            	@"Formatting double {0} with {{0:R}} to " +
            	"yield {0:R} (Round Trip)", 
            	figure);
        }

        #endregion

        #region percent(P)

        internal static string FormatPercent01(double 
        	figure)
        {
            return string.Format(
            	@"Formatting double {0} with {{0:P}} to yeild {0:P} (Percent)", 
            	figure);
        }

        #endregion

        #region hex(X)

        internal static string FormatHex01(int figure)
        {
            return string.Format(@"Hexedecimal {{0:X}}: {0:X}", figure);
        }

        #endregion

        #region padding

        internal static string PadInteger(int number)
        {
            string specifier = "0,0.00";
            return string.Format(@"PadInteger: {0}", number.ToString(specifier));
        }

        internal static string PadInteger02(int number)
        {            
            return string.Format(@"PadInteger: {0:000}", number);
        }

        internal static string PadInteger03(double number)
        {            
            return string.Format(@"PadDouble: {0:00.00}", number);
        }

        #endregion
    }
}