Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System Namespace
String Class
String Methods
Format Method
 Format Method (String, Object)

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
String..::.Format Method (String, Object)

Updated: October 2008

Replaces one or more format items in a specified string with the string representation of a specified object.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Shared Function Format ( _
    format As String, _
    arg0 As Object _
) As String
Visual Basic (Usage)
Dim format As String
Dim arg0 As Object
Dim returnValue As String

returnValue = String.Format(format, _
    arg0)
C#
public static string Format(
    string format,
    Object arg0
)
Visual C++
public:
static String^ Format(
    String^ format, 
    Object^ arg0
)
JScript
public static function Format(
    format : String, 
    arg0 : Object
) : String

Parameters

format
Type: System..::.String
A composite format string.
arg0
Type: System..::.Object
An object to format.

Return Value

Type: System..::.String
A copy of format in which any format items are replaced by the string representation of arg0.
ExceptionCondition
ArgumentNullException

format is nullNothingnullptra null reference (Nothing in Visual Basic).

FormatException

The format item in format is invalid.

-or-

The index of a format item is greater or less than zero.

This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its string representation and to embed that representation in a string. The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics.

The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to an object in the parameter list of this method. The formatting process replaces each format item with the string representation of the value of the corresponding object.

The syntax of a format item is as follows:

{index[,length][:formatString]}

Elements in square brackets are optional. The following table describes each element.

Element

Description

index

The zero-based position in the parameter list of the object to be formatted. If the object specified by index is nullNothingnullptra null reference (Nothing in Visual Basic), the format item is replaced by String..::.Empty. Because this overload has only a single object in its parameter list, the value of index must always be 0. If there is no parameter in the index position, a FormatException is thrown.

,length

The minimum number of characters in the string representation of the parameter. If positive, the parameter is right-aligned; if negative, it is left-aligned.

:formatString

A standard or custom format string that is supported by the parameter.

NoteNote:

For the standard and custom format strings used with date and time values, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. For the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. For the standard format strings used with enumerations, see Enumeration Format Strings.

The leading and trailing brace characters, '{' and '}', are required. To specify a single literal brace character in format, specify two leading or trailing brace characters; that is, "{{" or "}}".

Although the String..::.Format(String, Object) method has a single object in the parameter list, format can include more than one format item as long as each has the same index. In the following example, the format string includes two format items: one displays the decimal value of a number and the other displays its hexadecimal value.

Visual Basic
Module Example
   Public Sub Main()
      Dim values() As Short = { Int16.MinValue, -27, 0, 1042, Int16.MaxValue }
      Console.WriteLine("{0,10}  {1,10}", "Decimal", "Hex")
      Console.WriteLine()
      For Each value As Short In values
         Dim formatString As String = String.Format("{0,10:G}: {0,10:X}", value)
         Console.WriteLine(formatString)
      Next        
   End Sub
End Module
' The example displays the following output:
'       Decimal         Hex
'    
'        -32768:       8000
'           -27:       FFE5
'             0:          0
'          1042:        412
'         32767:       7FFF

C#
public class Example
{
   public static void Main()
   {
      short[] values= { Int16.MinValue, -27, 0, 1042, Int16.MaxValue };
      Console.WriteLine("{0,10}  {1,10}\n", "Decimal", "Hex");
      foreach (short value in values)
      {
         string formatString = String.Format("{0,10:G}: {0,10:X}", value);
         Console.WriteLine(formatString);
      }   
   }
}
// The example displays the following output:
//       Decimal         Hex
//    
//        -32768:       8000
//           -27:       FFE5
//             0:          0
//          1042:        412
//         32767:       7FFF

If the value of format is, "Thank you for your purchase of {0:####} copies of Microsoft®.NET (Core Reference).", and arg0 is an Int16 with the value 123, then the return value will be:

"Thank you for your purchase of 123 copies of Microsoft®.NET (Core Reference)."

If the value of format is, "Brad's dog has {0,-8:G} fleas.", arg0 is an Int16 with the value 42, (and in this example, underscores represent padding spaces) then the return value will be:

"Brad's dog has 42______ fleas."

The following example demonstrates the standard formatting specifiers for numbers, dates, and enumerations.

Visual Basic
' This code example demonstrates the String.Format() method.
' Formatting for this example uses the "en-US" culture.
Class Sample
   Public Enum Color
      Yellow = 1
      Blue = 2
      Green = 3
   End Enum 'Color

   Private Shared thisDate As DateTime = DateTime.Now

   Public Shared Sub Main()

      ' Store the output of the String.Format method in a string.
      Dim s As String = ""

      Console.Clear()

      ' Format a negative integer or floating-point number in various ways.
      Console.WriteLine("Standard Numeric Format Specifiers")
      s = String.Format("(C) Currency: . . . . . . . . {0:C}" & vbCrLf & _
                        "(D) Decimal:. . . . . . . . . {0:D}" & vbCrLf & _
                        "(E) Scientific: . . . . . . . {1:E}" & vbCrLf & _
                        "(F) Fixed point:. . . . . . . {1:F}" & vbCrLf & _
                        "(G) General:. . . . . . . . . {0:G}" & vbCrLf & _
                        "    (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
                        "(N) Number: . . . . . . . . . {0:N}" & vbCrLf & _
                        "(P) Percent:. . . . . . . . . {1:P}" & vbCrLf & _
                        "(R) Round-trip: . . . . . . . {1:R}" & vbCrLf & _
                        "(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _
                        - 123, - 123.45F)
      Console.WriteLine(s)

      ' Format the current date in various ways.
      Console.WriteLine("Standard DateTime Format Specifiers")
      s = String.Format("(d) Short date: . . . . . . . {0:d}" & vbCrLf & _
                        "(D) Long date:. . . . . . . . {0:D}" & vbCrLf & _
                        "(t) Short time: . . . . . . . {0:t}" & vbCrLf & _
                        "(T) Long time:. . . . . . . . {0:T}" & vbCrLf & _
                        "(f) Full date/short time: . . {0:f}" & vbCrLf & _
                        "(F) Full date/long time:. . . {0:F}" & vbCrLf & _
                        "(g) General date/short time:. {0:g}" & vbCrLf & _
                        "(G) General date/long time: . {0:G}" & vbCrLf & _
                        "    (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
                        "(M) Month:. . . . . . . . . . {0:M}" & vbCrLf & _
                        "(R) RFC1123:. . . . . . . . . {0:R}" & vbCrLf & _
                        "(s) Sortable: . . . . . . . . {0:s}" & vbCrLf & _
                        "(u) Universal sortable: . . . {0:u} (invariant)" & vbCrLf & _
                        "(U) Universal full date/time: {0:U}" & vbCrLf & _
                        "(Y) Year: . . . . . . . . . . {0:Y}" & vbCrLf, _
                        thisDate)
      Console.WriteLine(s)

      ' Format a Color enumeration value in various ways.
      Console.WriteLine("Standard Enumeration Format Specifiers")
      s = String.Format("(G) General:. . . . . . . . . {0:G}" & vbCrLf & _
                        "    (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
                        "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)" & vbCrLf & _
                        "(D) Decimal number: . . . . . {0:D}" & vbCrLf & _
                        "(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _
                        Color.Green)
      Console.WriteLine(s)
   End Sub 'Main
End Class 'Sample
'
'This code example produces the following results:
'
'Standard Numeric Format Specifiers
'(C) Currency: . . . . . . . . ($123.00)
'(D) Decimal:. . . . . . . . . -123
'(E) Scientific: . . . . . . . -1.234500E+002
'(F) Fixed point:. . . . . . . -123.45
'(G) General:. . . . . . . . . -123
'    (default):. . . . . . . . -123 (default = 'G')
'(N) Number: . . . . . . . . . -123.00
'(P) Percent:. . . . . . . . . -12,345.00 %
'(R) Round-trip: . . . . . . . -123.45
'(X) Hexadecimal:. . . . . . . FFFFFF85
'
'Standard DateTime Format Specifiers
'(d) Short date: . . . . . . . 6/26/2004
'(D) Long date:. . . . . . . . Saturday, June 26, 2004
'(t) Short time: . . . . . . . 8:11 PM
'(T) Long time:. . . . . . . . 8:11:04 PM
'(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
'(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
'(g) General date/short time:. 6/26/2004 8:11 PM
'(G) General date/long time: . 6/26/2004 8:11:04 PM
'    (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
'(M) Month:. . . . . . . . . . June 26
'(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
'(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
'(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
'(U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM
'(Y) Year: . . . . . . . . . . June, 2004
'
'Standard Enumeration Format Specifiers
'(G) General:. . . . . . . . . Green
'    (default):. . . . . . . . Green (default = 'G')
'(F) Flags:. . . . . . . . . . Green (flags or integer)
'(D) Decimal number: . . . . . 3
'(X) Hexadecimal:. . . . . . . 00000003
'

C#
// This code example demonstrates the String.Format() method.
// Formatting for this example uses the "en-US" culture.

using System;
class Sample 
{
    enum Color {Yellow = 1, Blue, Green};
    static DateTime thisDate = DateTime.Now;

    public static void Main() 
    {
// Store the output of the String.Format method in a string.
    string s = "";

    Console.Clear();

// Format a negative integer or floating-point number in various ways.
    Console.WriteLine("Standard Numeric Format Specifiers");
    s = String.Format(
        "(C) Currency: . . . . . . . . {0:C}\n" +
        "(D) Decimal:. . . . . . . . . {0:D}\n" +
        "(E) Scientific: . . . . . . . {1:E}\n" +
        "(F) Fixed point:. . . . . . . {1:F}\n" +
        "(G) General:. . . . . . . . . {0:G}\n" +
        "    (default):. . . . . . . . {0} (default = 'G')\n" +
        "(N) Number: . . . . . . . . . {0:N}\n" +
        "(P) Percent:. . . . . . . . . {1:P}\n" +
        "(R) Round-trip: . . . . . . . {1:R}\n" +
        "(X) Hexadecimal:. . . . . . . {0:X}\n",
        -123, -123.45f); 
    Console.WriteLine(s);

// Format the current date in various ways.
    Console.WriteLine("Standard DateTime Format Specifiers");
    s = String.Format(
        "(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 full date/time: {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(
        "(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);
    }
}
/*
This code example produces the following results:

Standard Numeric Format Specifiers
(C) Currency: . . . . . . . . ($123.00)
(D) Decimal:. . . . . . . . . -123
(E) Scientific: . . . . . . . -1.234500E+002
(F) Fixed point:. . . . . . . -123.45
(G) General:. . . . . . . . . -123
    (default):. . . . . . . . -123 (default = 'G')
(N) Number: . . . . . . . . . -123.00
(P) Percent:. . . . . . . . . -12,345.00 %
(R) Round-trip: . . . . . . . -123.45
(X) Hexadecimal:. . . . . . . FFFFFF85

Standard DateTime Format Specifiers
(d) Short date: . . . . . . . 6/26/2004
(D) Long date:. . . . . . . . Saturday, June 26, 2004
(t) Short time: . . . . . . . 8:11 PM
(T) Long time:. . . . . . . . 8:11:04 PM
(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
(g) General date/short time:. 6/26/2004 8:11 PM
(G) General date/long time: . 6/26/2004 8:11:04 PM
    (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
(M) Month:. . . . . . . . . . June 26
(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
(U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM
(Y) Year: . . . . . . . . . . June, 2004

Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
    (default):. . . . . . . . Green (default = 'G')
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003

*/

Visual C++
// This code example demonstrates the String.Format() method.
// Formatting for this example uses the "en-US" culture.

using namespace System;
using namespace System::Globalization;

enum class Color {Yellow = 1, Blue, Green};

int main(void)
{
    DateTime^ thisDate = DateTime::Now;
    // Store the output of the String::Format method in a string.
    String^ resultString = "";

    Console::Clear();

    // Format a negative integer or floating-point number in
    // various ways.
    Console::WriteLine("Standard Numeric Format Specifiers");
    resultString = String::Format(CultureInfo::CurrentCulture,
        "(C) Currency: . . . . . . . . {0:C}\n" +
        "(D) Decimal:. . . . . . . . . {0:D}\n" +
        "(E) Scientific: . . . . . . . {1:E}\n" +
        "(F) Fixed point:. . . . . . . {1:F}\n" +
        "(G) General:. . . . . . . . . {0:G}\n" +
        "    (default):. . . . . . . . {0} (default = 'G')\n" +
        "(N) Number: . . . . . . . . . {0:N}\n" +
        "(P) Percent:. . . . . . . . . {1:P}\n" +
        "(R) Round-trip: . . . . . . . {1:R}\n" +
        "(X) Hexadecimal:. . . . . . . {0:X}\n",
        -123, -123.45f);
    Console::WriteLine(resultString);

    // Format the current date in various ways.
    Console::WriteLine("Standard DateTime Format Specifiers");
    resultString = String::Format(CultureInfo::CurrentCulture,
        "(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 full date/time: {0:U}\n" +
        "(Y) Year: . . . . . . . . . . {0:Y}\n",
        thisDate);
    Console::WriteLine(resultString);

    // Format a Color enumeration value in various ways.
    Console::WriteLine("Standard Enumeration Format Specifiers");
    resultString = String::Format(CultureInfo::CurrentCulture,
        "(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(resultString);
};
/*
This code example produces the following results:

Standard Numeric Format Specifiers
(C) Currency: . . . . . . . . ($123.00)
(D) Decimal:. . . . . . . . . -123
(E) Scientific: . . . . . . . -1.234500E+002
(F) Fixed point:. . . . . . . -123.45
(G) General:. . . . . . . . . -123
(default):. . . . . . . . -123 (default = 'G')
(N) Number: . . . . . . . . . -123.00
(P) Percent:. . . . . . . . . -12,345.00 %
(R) Round-trip: . . . . . . . -123.45
(X) Hexadecimal:. . . . . . . FFFFFF85

Standard DateTime Format Specifiers
(d) Short date: . . . . . . . 6/26/2004
(D) Long date:. . . . . . . . Saturday, June 26, 2004
(t) Short time: . . . . . . . 8:11 PM
(T) Long time:. . . . . . . . 8:11:04 PM
(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
(g) General date/short time:. 6/26/2004 8:11 PM
(G) General date/long time: . 6/26/2004 8:11:04 PM
(default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
(M) Month:. . . . . . . . . . June 26
(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
(U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM
(Y) Year: . . . . . . . . . . June, 2004

Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
(default):. . . . . . . . Green (default = 'G')
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003

*/

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

October 2008

Expanded the Remarks section.

Customer feedback.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
What about {0.0} and {#.#}      CharlesForsyth   |   Edit   |   Show History

May I suggest adding example code similar to the following:

String.Format("(##.##): {0:##.##} {1:##.##} {2:##.##} {3:##.##} {4:##.##} {5:##.##}", 0.1, 1.2, 1, 1.234, 123.45, 123.456)


which results in:


(##.##): .1 1.2 1 1.23 123.45 123.46

...And...


String.Format("(00.00): {0:00.00} {1:00.00} {2:00.00} {3:00.00} {4:00.00} {5:00.00}", 0.1, 1.2, 1, 1.234, 123.45, 123.456)


Which would result in:


(00.00): 00.10 01.20 01.00 01.23 123.45 123.46

String Formatting - using PowerShell      Thomas Lee   |   Edit   |   Show History
<#  

.SYNOPSIS

Demonstrates simple string formatting using Powershell

.DESCRIPTION

This script formats 5 numbers in Decimal, Hex and Currency and is

an adaptation of the C# sample in the MSDN Library

.NOTES

Author : Thomas Lee - tfl@psp.co.uk

.LINK

http:////msdn.microsoft.com/en-us/library/fht0f5be.aspx

http://www.pshscripts.blogspot.com

.EXAMPLE

PS C:\foo> .\Get-StringFormat1.ps1

Decimal Hex Currency

-32768: 8000 ($32,768.00)

-27: FFFFFFE5 ($27.00)

0: 0 $0.00

1042: 412 $1,042.00

32767: 7FFF $32,767.00

#>

# Create an array of values to display

$values = [Int16]::MinValue, -27, 0, 1042, [Int16]::maxValue


# Print header line then values

"{0,10} {1,10} {2,14}" -f "Decimal", "Hex", "Currency"

foreach ($value in $values) {

"{0,10:G}: {0,10:X} {0,14:C}" -f $value

}

Formating with PowerShell - second sample      Thomas Lee   |   Edit   |   Show History
  
<#
.SYNOPSIS
Demonstrates string formatting using Powershell
.DESCRIPTION
This script formats 2 numbers in a variety of ways, also
formats an enum.
An adaptation of the C# sample in the MSDN Library. However,
this script uses a built-in enum, and displays two.
.NOTES
Author : Thomas Lee - tfl@psp.co.uk
.INPUTTYPE
This script has no effective parameters.
.RETURNVALUE
This script produces output as shown in the example
.LINK
http:////msdn.microsoft.com/en-us/library/fht0f5be.aspx
http://www.pshscripts.blogspot.com
.EXAMPLE
PS C:\foo> .\'Get-StringFormat2.ps1'
(C) Currency: . . . . . . . . ($123.00)
(D) Decimal:. . . . . . . . . -123
(E) Scientific: . . . . . . . -1.234500E+002
(F) Fixed point:. . . . . . . -123.45
(G) General:. . . . . . . . . -123
(default):. . . . . . . . -123
(N) Number: . . . . . . . . . -123.00
(P) Percent:. . . . . . . . . -12,345.00 %
(R) Round-trip: . . . . . . . -123.45
(X) Hexadecimal:. . . . . . . FFFFFF85
Date Format
(d) Short date: . . . . . . . 12/25/2008
(D) Long date:. . . . . . . . Thursday, December 25, 2008
(t) Short time: . . . . . . . 2:12 PM
(T) Long time:. . . . . . . . 2:12:55 PM
(f) Full date/short time: . . Thursday, December 25, 2008 2:12 PM
(F) Full date/long time:. . . Thursday, December 25, 2008 2:12:55 PM
(g) General date/short time:. 12/25/2008 2:12 PM
(G) General date/long time: . 12/25/2008 2:12:55 PM
(default):. . . . . . . . 12/25/2008 2:12:55 PM
(M) Month:. . . . . . . . . . December 25
(R) RFC1123:. . . . . . . . . Thu, 25 Dec 2008 14:12:55 GMT
(s) Sortable: . . . . . . . . 2008-12-25T14:12:55
(u) Universal sortable: . . . 2008-12-25 14:12:55Z
(U) Universal full date/time: Thursday, December 25, 2008 2:12:55 PM
(Y) Year: . . . . . . . . . . December, 2008
Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
(default):. . . . . . . . Green
(F) Flags:. . . . . . . . . . Green
(D) Decimal number: . . . . . 79
(X) Hexadecimal:. . . . . . . 0000004F
Standard Enumeration Format Specifiers (again)
(G) General:. . . . . . . . . Gainsboro
(default):. . . . . . . . Gainsboro
(F) Flags:. . . . . . . . . . Gainsboro
(D) Decimal number: . . . . . 74
(X) Hexadecimal:. . . . . . . 0000004A
.EXAMPLE
PS C:\Users\tfl> Get-Help .\Get-StringFormat2.ps1'
Left as an exercise for the reader.
#>
##
# Start of script
##
#Format a negative integer or floating-point number in various ways.
"Standard Numeric Format Specifiers"
"(C) Currency: . . . . . . . . {0:C}" -f -123
"(D) Decimal:. . . . . . . . . {0:D}" -f -123
"(E) Scientific: . . . . . . . {0:E}" -f -123.45
"(F) Fixed point:. . . . . . . {0:F}" -f -123.45
"(G) General:. . . . . . . . . {0:G}" -f -123
"(default):. . . . . . . . {0} " -f -123
"(N) Number: . . . . . . . . . {0:N}" -f -123
"(P) Percent:. . . . . . . . . {0:P}" -f -123.45
"(R) Round-trip: . . . . . . . {0:R}" -f -123.45
"(X) Hexadecimal:. . . . . . . {0:X}" -f -123
""
# Format the current date in various ways.
$today = Get-Date
"Date Format"
"(d) Short date: . . . . . . . {0:d}" -f $today
"(D) Long date:. . . . . . . . {0:D}" -f $today
"(t) Short time: . . . . . . . {0:t}" -f $today
"(T) Long time:. . . . . . . . {0:T}" -f $today
"(f) Full date/short time: . . {0:f}" -f $today
"(F) Full date/long time:. . . {0:F}" -f $today
"(g) General date/short time:. {0:g}" -f $today
"(G) General date/long time: . {0:G}" -f $today
" (default):. . . . . . . . {0} " -f $today
"(M) Month:. . . . . . . . . . {0:M}" -f $today
"(R) RFC1123:. . . . . . . . . {0:R}" -f $today
"(s) Sortable: . . . . . . . . {0:s}" -f $today
"(u) Universal sortable: . . . {0:u}" -f $today
"(U) Universal full date/time: {0:U}" -f $today
"(Y) Year: . . . . . . . . . . {0:Y}" -f $today
""
# Format a Color enumeration value in various ways.
"Standard Enumeration Format Specifiers"
"(G) General:. . . . . . . . . {0:G}" -f [system.Drawing.KnownColor]::Green
" (default):. . . . . . . . {0}" -f [system.Drawing.KnownColor]::Green
"(F) Flags:. . . . . . . . . . {0:F} " -f [system.Drawing.KnownColor]::Green
"(D) Decimal number: . . . . . {0:D} " -f [system.Drawing.KnownColor]::Green
"(X) Hexadecimal:. . . . . . . {0:X}" -f [system.Drawing.KnownColor]::Green
"Standard Enumeration Format Specifiers (again)"
"(G) General:. . . . . . . . . {0:G}" -f [system.Drawing.KnownColor]::Gainsboro
" (default):. . . . . . . . {0}" -f [system.Drawing.KnownColor]::Gainsboro
"(F) Flags:. . . . . . . . . . {0:F} " -f [system.Drawing.KnownColor]::Gainsboro
"(D) Decimal number: . . . . . {0:D} " -f [system.Drawing.KnownColor]::Gainsboro
"(X) Hexadecimal:. . . . . . . {0:X}" -f [system.Drawing.KnownColor]::Gainsboro
### End of Script
# Format the curre
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker