Format

Top  Previous  Next

Function

Parameters

Return value

Format

string format,

params object[] args

string

 

Replaces the format item in a specified "format" string with the value of a corresponding Object instance in a specified "args" array.

 

For example, the following function call:

 

Format("Name = {0}, hours = {1:hh}", myName, DateTime.Now)

 

contains the following format items: "{0}" and "{1:hh}". They will be replaced with values of myName and DateTime.Now. The result may look as follows:

 

Name = Alex, hours = 12

 

Each format item takes the following form:

 

{index[,alignment][:formatString]}

 

index - a zero-based integer that indicates which element in a list of objects to format;
alignment - an optional integer indicating the minimum width of the region to contain the formatted value. If the length of the formatted value is less than alignment, then the region is padded with spaces. If alignment is negative, the formatted value is left justified in the region; if alignment is positive, the formatted value is right justified;
formatString -an optional string of format specifiers.

 

The following table describes the standard numeric format strings.

 

Format Specifier

Name

Description

C or c

Currency

The number is converted to a string that represents a currency amount.

 

Format("{0:C}", 10) = "$10.00"

D or d

Decimal

This format is supported for integral types only. The number is converted to a string of decimal digits (0-9).

 

Format("{0:D}", 10) = "10"

E or e

Scientific

The number is converted to a string of the form

"-d.ddd…E+ddd" or "-d.ddd…e+ddd", where each 'd' indicates a digit (0-9).

 

Format("{0:E}", 10) = "1,000000E+001"

F or f

Fixed-point

The number is converted to a string of the form "-ddd.ddd…" where each 'd' indicates a digit (0-9).

 

Format("{0:F}", 10) = "10.00"

G or g

General

The number is converted to the most compact notation.

 

Format("{0:G}", 10) = "10"

N or n

Number

The number is converted to a string of the form "-d,ddd,ddd.ddd…", where each 'd' indicates a digit (0-9).

 

Format("{0:N}", 1234.56) = "1,234.56"

P or p

Percent

The number is converted to a string that represents a percent. The converted number is multiplied by 100 in order to be presented as a percentage.

 

Format("{0:P}", 0.15) = "15.00%"

X or x

Hexadecimal

The number is converted to a string of hexadecimal digits. The case of the format specifier indicates whether to use uppercase or lowercase characters for the hexadecimal digits greater than 9. For example, use 'X' to produce "ABCDEF", and 'x' to produce "abcdef".

 

Format("{0:X}", 26) = "1A"

 

If you format the floating-point values, you may indicate a number of decimal places after the format string:

 

Format("{0:C1}", 12.23) = "$12.2"

 

If the standard numeric format specifiers do not provide the type of formatting you require, you can use custom format strings:

 

Format character

Description

0

Zero placeholder. If the value being formatted has a digit in the position where the '0' appears in the format string, then that digit is copied to the result string. The position of the leftmost '0' before the decimal point and the rightmost '0' after the decimal point determines the range of digits that are always present in the result string.

#

Digit placeholder. If the value being formatted has a digit in the position where the '#' appears in the format string, then that digit is copied to the result string. Otherwise, nothing is stored in that position in the result string.

.

Decimal point. The first '.' character in the format string determines the location of the decimal separator in the formatted value.

,

Thousand separator. If the format string contains a ',' character, then the output will have thousand separators inserted between each group of three digits to the left of the decimal separator.

%

Percentage placeholder. The presence of a '%' character in a format string causes a number to be multiplied by 100 before it is formatted.

;

Section separator. The ';' character is used to separate sections for positive, negative, and zero numbers in the format string.

 

Examples of use:

 

Format("{0:$#,##0.00}", 1024.25) = "$1,024.25"

Format("{0:00%}", 0.25) = "25%"

Format("{0:$#,##0.00;($#,##0.00);Zero}", 1024.25) = "$1,024.25"

Format("{0:$#,##0.00;($#,##0.00);Zero}", -1024.25) = "($1,024.25)"

Format("{0:$#,##0.00;($#,##0.00);Zero}", 0) = "Zero"

 

 

The following table describes the standard format specifiers for formatting the DateTime values:

 

Format Specifier

Name

Example

d

Short date pattern

"8/9/2009"

D

Long date pattern

"Sunday, August 09, 2009"

f

Full date/time pattern

(short time)

"Sunday, August 09, 2009 2:44 PM"

F

Full date/time pattern

(long time)

"Sunday, August 09, 2009 2:44:01 PM"

g

General date/time pattern

(short time)

"8/9/2009 2:44 PM"

G

General date/time pattern

(long time)

"8/9/2009 2:44:01 PM"

t

Short time pattern

"2:44 PM"

T

Long time pattern

"2:44:01 PM"

 

The following table describes the custom date/time format specifiers and the results they produce.

 

Format Specifier

Description

d

Displays the current day of the month, measured as a number between 1 and 31, inclusive. If the day is a single digit only (1-9), then it is displayed as a single digit.

dd

Displays the current day of the month, measured as a number between 1 and 31, inclusive. If the day is a single digit only (1-9), it is formatted with a preceding 0 (01-09).

ddd

Displays the abbreviated name of the day.

dddd

Displays the full name of the day.

f or F

Displays the most significant digit of the seconds fraction.

h

Displays the hour in the range 1-12. If the hour is a single digit (1-9), it is displayed as a single digit.

hh

Displays the hour in the range 1-12. If the hour is a single digit (1-9), it is formatted with a preceding 0 (01-09).

H

Displays the hour in the range 0-23. If the hour is a single digit (1-9), it is displayed as a single digit.

HH

Displays the hour in the range 0-23. If the hour is a single digit (1-9), it is formatted with a preceding 0 (01-09).

m

Displays the minute in the range 0-59. If the minute is a single digit (0-9), it is displayed as a single digit.

mm

Displays the minute in the range 0-59. If the minute is a single digit (0-9), it is formatted with a preceding 0 (01-09).

M

Displays the month, measured as a number between 1 and 12, inclusive. If the month is a single digit (1-9), it is displayed as a single digit.

MM

Displays the month, measured as a number between 1 and 12, inclusive. If the month is a single digit (1-9), it is formatted with a preceding 0 (01-09).

MMM

Displays the abbreviated name of the month.

MMMM

Displays the full name of the month.

s

Displays the seconds in the range 0-59. If the second is a single digit (0-9), it is displayed as a single digit only.

ss

Displays the seconds in the range 0-59. If the second is a single digit (0-9), it is formatted with a preceding 0 (01-09).

t

Displays the first character of the A.M./P.M. designator.

tt

Displays the A.M./P.M. designator.

y

Displays the year as a maximum two-digit number. The first two digits of the year are omitted. If the year is a single digit (1-9), it is displayed as a single digit.

yy

Displays the year as a maximum two-digit number. The first two digits of the year are omitted. If the year is a single digit (1-9), it is formatted with a preceding 0 (01-09).

yyyy

Displays the year, including the century. If the year is less than four digits in length, then preceding zeros are appended as necessary to make the displayed year four digits long.

z

Displays the time zone offset for the system's current time zone in whole hours only. The offset is always displayed with a leading sign (zero is displayed as "+0"), indicating hours ahead of Greenwich mean time (+) or hours behind Greenwich mean time (-). The range of values is –12 to +13. If the offset is a single digit (0-9), it is displayed as a single digit with the appropriate leading sign.

zz

Displays the time zone offset for the system's current time zone in whole hours only. The offset is always displayed with a leading or trailing sign (zero is displayed as "+00"), indicating hours ahead of Greenwich mean time (+) or hours behind Greenwich mean time (-). The range of values is –12 to +13. If the offset is a single digit (0-9), it is formatted with a preceding 0 (01-09) with the appropriate leading sign.

zzz

Displays the time zone offset for the system's current time zone in hours and minutes. The offset is always displayed with a leading or trailing sign (zero is displayed as "+00:00"), indicating hours ahead of Greenwich mean time (+) or hours behind Greenwich mean time (-). The range of values is –12:00 to +13:00. If the offset is a single digit (0-9), it is formatted with a preceding 0 (01-09) with the appropriate leading sign.

:

Time separator.

/

Date separator.

 

Examples of use:

 

Format("{0:d MMM yyyy}", DateTime.Now) = "9 Aug 2009"

Format("{0:MM/dd/yyyy}", DateTime.Now) = "08/09/2009"

Format("{0:MMMM, d}", DateTime.Now) = "August, 9"

Format("{0:HH:mm}", DateTime.Now) = "16:07"

Format("{0:MM/dd/yyyy hh:mm tt}", DateTime.Now) = "08/09/2009 04:07 PM"