.NET String Formatting

String Formatting Syntax

The format string supported by the format (-f ) operator is a string that contains format items. Each format item takes the form of:

{index[,alignment][:formatString]}

<index> represents the zero-based index of the item in the object array following the format operator.

<alignment> is optional, and represents the alignment of the item. A positive number aligns the item to the right of a field of the specified width. A negative number aligns the item to the left of a field of the specifed width.

<formatString> is optional, and formats the item using to that type’s specific format string syntax (as laid out below.)

Standard Numeric Format Strings

All format specifiers may be followed by a number between 0 and 99 to control the precision of the formatting.

Standard Numeric Format Strings

Format Specifier

Name

Description

Example

C or c

Currency

A currency amount.

PS >"{0:C}" -f 1.23
$1.23

D or d

Decimal

A decimal amount (for integral types.) The precision specifier controls the minimum number of digits in the result.

PS >"{0:D4}" -f 2
0002

E or e

Scientific

Scientific (exponential) notation. The precision specifier controls the number of digits past the decimal point.

PS >"{0:E3}" -f [Math]::Pi
3.142E+000

F or f

Fixed-point

Fixed point notation. The precision specifier controls the number of digits past the decimal point.

PS >"{0:F3}" -f [Math]::Pi
3.142

G or g

General

The most compact representation (between fixed point and scientific) of the number. The precision specifier controls the number of significant digits.

PS >"{0:G3}" -f [Math]::Pi
3.14
PS >"{0:G3}" -f 1mb
1.05E+06

N or n

Number

The human readable form of the number, which includes separators between number groups. The precision specifier controls the number of digits past the decimal point.

PS >"{0:N4}" -f 1mb
1,048,576.0000

P or p

Percent

The number (generally between 0 and 1) represented as a percentage. The precision specifier controls the number of digits past the decimal point.

PS >"{0:P4}" -f 0.67
67.0000 %

R or r

Round-trip

The Single or Double number formatted with a precision that guarantees the string (when parsed) will result in the original number again.

PS >"{0:R}" -f (1mb/2.0)
524288
PS >"{0:R}" -f (1mb/9.0)
116508.44444444444

X or x

Hexidecimal

The number converted to a string of hexadecimal digits. The case of the specifier controls the case of the resulting hexadecimal digits. The precision specifier controls the minimum number of digits is the resulting string.

PS >"{0:X4}" -f 1324
052C

Custom Numeric Format Strings

You may use custom numeric strings to format numbers in ways not supported by the standard format strings.

Custom Numeric Format Strings

Format Specifier

Name

Description

Example

0

Zero placeholder

Specifies the precision and width of a number string. Zeroes not matched by digits in the original number are output as zeroes.

PS >"{0:00.0}" -f 4.12341234
04.1

#

Digit placeholder

Specifies the precision and width of a number string. # symbols not matched by digits in the input number are not output.

PS >"{0:##.#}" -f 4.12341234
4.1

.

Decimal point

Determines the location of the decimal separator.

PS >"{0:##.#}" -f 4.12341234
4.1

,

Thousands separator

When placed between a zero or digit placeholder before the decimal point in a formatting string, adds the separator character between number groups.

PS >"{0:#,#.#}" -f
1234.121234
1,234.1

,

Number scaling

When placed before the literal (or implicit) decimal point in a formatting string, divides the input by 1000. You may apply this format specifier more than once.

PS >"{0:##,,.000}" -f 1048576
1.049

%

Percentage placeholder

Multiplies the input by 100, and inserts the percent sign where shown in the format specifier.

PS >"{0:%##.000}" -f .68
%68.000
E0
E+0
E-0
e0
e+0
e-0

Scientific notation

Displays the input in scientific notation. The number of zeroes that follow the E define the minimum length of the exponent field.

PS >"{0:##.#E000}" -f 2.71828
27.2E-001
'text'
"text"

Literal string

Inserts the provided text literally into the output without affecting formatting.

PS >"{0:#.00'##'}" -f 2.71828
2.72##

;

Section separator

Allows for conditional formatting.

If your format specifier contains no section separators, then the formatting statement applies to all input.

If your format specifier contains one separator (creating two sections,) then the first section applies to positive numbers and zero. The second section applies to negative numbers.

If your format specifier contains two separators (creating three sections,) then the sections apply to positive numbers, negative numbers, and zero.

PS >"{0:POS;NEG;ZERO}" -f -14
NEG

Other

Other character

Inserts the provided text literally into the output without affecting formatting.

PS >"{0:$## Please}" -f 14
$14 Please

Standard DateTime Format Strings

Converts a DateTime object to one of several standard formats.

Standard DateTime Format Strings

Format Specifier

Name

Description

Example

d

Short date

The culture’s short date format.

PS >"{0:d}" -f [DateTime]
"01/23/4567"
1/23/4567

D

Long date

The culture’s long date format.

PS >"{0:D}" -f [DateTime]
"01/23/4567"
Friday, January 23, 4567

f

Full date / short time

Combines the long date and short time format patterns.

PS >"{0:f}" -f [DateTime]
"01/23/4567"
Friday, January 23, 4567 12:00 AM

F

Full date / long time

Combines the long date and long time format patterns.

PS >"{0:F}" -f [DateTime]
"01/23/4567"
Friday, January 23, 4567 12:00:00 AM

g

General date / short time

Combines the short date and short time format patterns.

PS >"{0:g}" -f [DateTime]
"01/23/4567"
1/23/4567 12:00 AM

G

General date / long time

Combines the short date and long time format patterns.

PS >"{0:G}" -f [DateTime]
"01/23/4567"
1/23/4567 12:00:00 AM

M or m

Month day

The culture’s MonthDay format.

PS >"{0:M}" -f [DateTime]
"01/23/4567"
January 23

o

Round-trip date / time

The date formatted with a pattern that guarantees the string (when parsed) will result in the original DateTime again.

PS >"{0:o}" -f [DateTime]
"01/23/4567"
4567-01-23T00:00:00.0000000

R or r

RFC1123

The standard RFC1123 format pattern.

PS >"{0:R}" -f [DateTime]
"01/23/4567"
Fri, 23 Jan 4567 00:00:00 GMT

s

Sortable

Sortable format pattern. Conforms to ISO 8601, and provides output suitable for sorting.

PS >"{0:s}" -f [DateTime]
"01/23/4567"
4567-01-23T00:00:00

t

Short time

The culture’s ShortTime format.

PS >"{0:t}" -f [DateTime]
"01/23/4567"
12:00 AM

T

Long time

The culture’s LongTime format.

PS >"{0:T}" -f [DateTime]
"01/23/4567"
12:00:00 AM

u

Universal sortable

The culture’s UniversalSortableDateTime format applied to the UTC equivalent of the input.

PS >"{0:u}" -f [DateTime]
"01/23/4567"
4567-01-23 00:00:00Z

U

Universal sortable

The culture’s FullDateTime format applied to the UTC equivalent of the input.

PS >"{0:U}" -f [DateTime]
"01/23/4567"
Friday, January 23, 4567 8:00:00 AM

Y or y

Year month

The culture’s YearMonth format.

PS >"{0:Y}" -f [DateTime]
"01/23/4567"
January, 4567

Custom DateTime Format Strings

You may use custom DateTime format strings to format dates in ways not supported by the standard format strings. Note: single-character format specifiers are interpreted as a standard DateTime formatting string unless used with other formatting specifiers.

Custom DateTime Format Strings

Format Specifier

Description

Example

d

Day of the month as a number between 1 and 31. Represents single-digit days without a leading zero.

PS >"{0:d dd ddd dddd}" -f
    [DateTime] "01/02/4567"
2 02 Fri Friday

dd

Day of the month as a number between 1 and 31. Represents single-digit days with a leading zero.

PS >"{0:d dd ddd dddd}" -f
    [DateTime] "01/02/4567"
2 02 Fri Friday

ddd

Abbreviated name of the day of week.

PS >"{0:d dd ddd dddd}" -f
    [DateTime] "01/02/4567"
2 02 Fri Friday

dddd

Full name of the day of the week.

PS >"{0:d dd ddd dddd}" -f
    [DateTime] "01/02/4567"
2 02 Fri Friday

f

Most significant digit of the seconds fraction

PS >"{0:f ff fff ffff}" -f
    [DateTime] "01/02/4567"
0 00 000 0000

ff

Two most significant digit of the seconds fraction.

PS >"{0:f ff fff ffff}" -f
    [DateTime] "01/02/4567"
0 00 000 0000

fff

Three most significant digit of the seconds fraction.

PS >"{0:f ff fff ffff}" -f
    [DateTime] "01/02/4567"
0 00 000 0000

ffff

Four most significant digit of the seconds fraction.

PS >"{0:f ff fff ffff}" -f
    [DateTime] "01/02/4567"
0 00 000 0000

fffff

Five most significant digit of the seconds fraction.

PS >"{0:fffff ffffff fffffff}" -f
    [DateTime] "01/02/4567"
00000 000000 0000000

ffffff

Six most significant digit of the seconds fraction.

PS >"{0:fffff ffffff fffffff}" -f
    [DateTime] "01/02/4567"
00000 000000 0000000

fffffff

Seven most significant digit of the seconds fraction.

PS >"{0:fffff ffffff fffffff}" -f
    [DateTime] "01/02/4567"
00000 000000 0000000

F

Most significant digit of the seconds fraction. Displays nothing if the number is zero.

PS >"{0:F FF FFF FFFF}" -f
    [DateTime]::Now
6 66 669 6696

PS >"{0:|F FF FFF FFFF|}" -f
    [DateTime] "01/02/4567"
|   |

FF

Two most significant digit of the seconds fraction. Displays nothing if the number is zero.

PS >"{0:F FF FFF FFFF}" -f
    [DateTime]::Now
6 66 669 6696

PS >"{0:|F FF FFF FFFF|}" -f
    [DateTime] "01/02/4567"
|   |

FFF

Three most significant digit of the seconds fraction. Displays nothing if the number is zero.

PS >"{0:F FF FFF FFFF}" -f
    [DateTime]::Now
6 66 669 6696

PS >"{0:|F FF FFF FFFF|}" -f
    [DateTime] "01/02/4567"
|   |

FFFF

Four most significant digit of the seconds fraction. Displays nothing if the number is zero.

PS >"{0:F FF FFF FFFF}" -f
    [DateTime]::Now
6 66 669 6696

PS >"{0:|F FF FFF FFFF|}" -f
    [DateTime] "01/02/4567"
|   |

FFFFF

Five most significant digit of the seconds fraction. Displays nothing if the number is zero.

PS >"{0:FFFFF FFFFFF FFFFFFF}" -f
    [DateTime]::Now
1071 107106 1071068

PS >"{0:|FFFFF FFFFFF FFFFFFF|}" -f
    [DateTime] "01/02/4567"
|   |

FFFFFF

Six most significant digit of the seconds fraction. Displays nothing if the number is zero.

PS >"{0:FFFFF FFFFFF FFFFFFF}" -f
    [DateTime]::Now
1071 107106 1071068

PS >"{0:|FFFFF FFFFFF FFFFFFF|}" -f
    [DateTime] "01/02/4567"
|   |

FFFFFFF

Seven most significant digit of the seconds fraction. Displays nothing if the number is zero.

PS >"{0:FFFFF FFFFFF FFFFFFF}" -f
    [DateTime]::Now
1071 107106 1071068

PS >"{0:|FFFFF FFFFFF FFFFFFF|}" -f
    [DateTime] "01/02/4567"
|   |

g or gg

Era (i.e., A.D)

PS >"{0:gg}" -f [DateTime]
"01/02/4567"
A.D.

h

Hours, as a number between 1 and 12. Single digits do not include a leading zero.

PS >"{0:%h}" -f
    [DateTime] "01/02/4567 4:00pm"
4

hh

Hours, as a number between 01 and 12. Single digits include a leading zero. Note: this is interpreted as a standard DateTime formatting string unless used with other formatting specifiers.

PS >"{0:hh}" -f
    [DateTime] "01/02/4567 4:00pm
04

H

Hours, as a number between 0 and 23. Single digits do not include a leading zero.

PS >"{0:%H}" -f
    [DateTime] "01/02/4567 4:00pm"
16

HH

Hours, as a number between 00 and 23. Single digits include a leading zero.

PS >"{0:HH}" -f
    [DateTime] "01/02/4567 4:00am"
04

K

DateTime.Kind specifier that corresponds to the kind (ie: Local, Utc, or Unspecified) of input date.

PS >"{0: K}" -f
    [DateTime]::Now.ToUniversalTime()
 Z

m

Minute, as a number between 0 and 59. Single digits do not include a leading zero.

PS >"{0: m}" -f [DateTime]::Now
 7

mm

Minute, as a number between 00 and 59. Single digits include a leading zero.

PS >"{0:mm}" -f [DateTime]::Now
08

M

Month, as a number between 1 and 12. Single digits do not include a leading zero.

PS >"{0:M MM MMM MMMM}" -f
    [DateTime] "01/02/4567"
1 01 Jan January

MM

Month, as a number between 01 and 12. Single digits include a leading zero.

PS >"{0:M MM MMM MMMM}" -f
    [DateTime] "01/02/4567"
1 01 Jan January

MMM

Abbreviated month name.

PS >"{0:M MM MMM MMMM}" -f
    [DateTime] "01/02/4567"
1 01 Jan January

MMMM

Full month name.

PS >"{0:M MM MMM MMMM}" -f
    [DateTime] "01/02/4567"
1 01 Jan January

s

Seconds, as a number between 0 and 59. Single digits do not include a leading zero.

PS > "{0:s ss t tt}" -f
    [DateTime]::Now
3 03 A AM

ss

Seconds, as a number between 00 and 59. Single digits include a leading zero.

PS > "{0:s ss t tt}" -f
    [DateTime]::Now
3 03 A AM

t

First character of the A.M. / P.M. designator.

PS > "{0:s ss t tt}" -f
    [DateTime]::Now
3 03 A AM

tt

A.M. / P.M designator.

PS > "{0:s ss t tt}" -f
    [DateTime]::Now
3 03 A AM

y

Year, in (at most) 2 digits.

PS >"{0:y yy yyy yyyy yyyyy}" -f
    [DateTime] "01/02/4567"
67 67 4567 4567 04567

yyy

Year, in (at most) 3 digits.

PS >"{0:y yy yyy yyyy yyyyy}" -f
    [DateTime] "01/02/4567"
67 67 4567 4567 04567

yyyy

Year, in (at most) 4 digits.

PS >"{0:y yy yyy yyyy yyyyy}" -f
    [DateTime] "01/02/4567"
67 67 4567 4567 04567

yyyyy

Year, in (at most) 5 digits.

PS >"{0:y yy yyy yyyy yyyyy}" -f
    [DateTime] "01/02/4567"
67 67 4567 4567 04567

z

Signed time zone offset from GMT. Does not include a leading zero.

PS >"{0:z zz zzz}" -f [DateTime]::Now
-7 -07 -07:00

zz

Signed time zone offset from GMT. Includes a leading zero.

PS >"{0:z zz zzz}" -f [DateTime]::Now
-7 -07 -07:00

zzz

Signed time-zone offset from GMT, measured in hours and minutes.

PS >"{0:z zz zzz}" -f [DateTime]::Now
-7 -07 -07:00

:

Time separator.

PS > "{0:y/m/d h:m:s}" -f
    [DateTime] "01/02/4567 4:00pm"
67/0/2 4:0:0

/

Date separator.

PS > "{0:y/m/d h:m:s}" -f
    [DateTime] "01/02/4567 4:00pm"
67/0/2 4:0:0
"text"
'text'

Inserts the provided text literally into the output without affecting formatting.

PS >"{0:'Day: 'dddd}" -f
    [DateTime]::Now
Day: Monday

%c

Syntax allowing for single-character custom formatting specifiers. The % sign is not added to the output.

PS >"{0:%h}" -f
    [DateTime] "01/02/4567 4:00pm"
4

Other

Inserts the provided text literally into the output without affecting formatting.

PS >"{0:dddd!}" -f [DateTime]::Now
Monday!
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.12.172