Format Strings
  • 16 Apr 2024
  • 4 Minutes to read
  • Dark
    Light

Format Strings

  • Dark
    Light

Article summary

General

Format strings are used when converting a number or date-time to a string (textual) representation with a given format.

Example: The number 1256 can be converted to a string in the format 1.256 where the dot is a group separator with the format string #.##0.

Number Format Strings

A full description of format strings that may be used when converting numbers to strings can be found in the  Standard Numeric Format Strings and  Custom Numeric Format Strings articles of the .NET language reference.

Standard Numeric Format Strings

These format strings are used to format a number to a format defined in the regional settings of the operating system in which Mail & Deploy runs (i.e. the Windows culture settings). The following table contains a few examples of standard numeric format strings:

Format String

Description
CFormats the number as a currency with the number of decimals and currency symbol as defined in the currently active culture settings. You can override the default number of decimals by appending the number of required decimals after the C (e.g. C3 will format the currency with 3 decimals).
PFormats the number as a percentage by multiplying it with 100 and displaying a % symbol.
XFormats the number as a hexa-decimal string.

Custom Numeric Format Strings

These format strings allow a more detailed control over how a number is formatted. They can be constructed from the following placeholders:

Placeholder

Description
0The zero placeholder will be replaced by the actual digit of the formatted number if one is present; otherwise a zero appears in the result string.
#The # placeholder will be replaced by the actual digit of the formatted number if one is present; otherwise, no digit appears in the result string.
.The decimal placeholder can be used to specify the location of the decimal separator in the result string. The placeholder to be used is always a dot and is independent of the active culture settings, which will only affect the decimal separator used in the result string.
,The comma placeholder can be used to specify the location of the group separator in the result string. The placeholder to be used is always a comma and is independent of the active culture settings, which will only affect the group separator used in the result string.

The following example formats a number to always show to decimals and at least one digit before the decimal. Groups of digits before the decimal will be separated using the group separator of the active culture settings:

Format String

#,##0.00

The following example formats a number to always show three digits but no decimals:

Format String

000

Date-Time Format Strings

A full description of format strings that may be used when converting date-times to strings can be found in the  Standard Date and Time Format Strings and  Custom Date and Time Format Strings articles of the .NET language reference.

Standard Numeric Format Strings

These format strings are used to format a date-time to a format defined in the regional settings of the operating system in which Mail & Deploy runs (i.e. the Windows culture settings). The following table contains a few examples of standard date-time format strings:

Format String

Description
dFormats the date-time using the short date pattern (e.g. 6/15/2009 for US culture settings).
DFormats the date-time using the long date pattern (e.g. Monday, June 15, 2009 for US culture settings).
fFormats the date-time using the full date-time pattern with short time (e.g. Monday, June 15, 2009 1:45 PM for US culture settings).
FFormats the date-time using the full date-time pattern with long time (e.g. Monday, June 15, 2009 1:45:30 PM for US culture settings).

Custom Date-Time Format Strings

These format strings allow a more detailed control over how a date-time is formatted. They can be constructed from the following placeholders:

Placeholder

Description
dThis placeholder will be replaced by the number of the day (e.g. 1 or 30) either as a single digit or two digits.
ddThis placeholder will be replaced by the number of the day (e.g. 01 or 30) with two digits.
MThis placeholder will be replaced by the month from 1 to 12 (e.g. 1 or 11) either as a single digit or two digits.
MMThis placeholder will be replaced by the month from 1 to 12 (e.g. 01 or 11) with two digits.
yyyyThis placeholder will be replaced by the year with four digits.
hThis placeholder will be replaced by the hour from 1 to 12 (e.g. 1 or 11) either as a single digit or two digits.
hhThis placeholder will be replaced by the hour from 1 to 12 (e.g. 01 or 11) with two digits.
HThis placeholder will be replaced by the hour from 1 to 24 (e.g. 1 or 11) either as a single digit or two digits.
HHThis placeholder will be replaced by the hour from 1 to 24 (e.g. 01 or 11) with two digits.
mThis placeholder will be replaced by the minute from 1 to 59 (e.g. 1 or 11) either as a single digit or two digits.
mmTh.is placeholder will be replaced by the minute from 1 to 59 (e.g. 01 or 11) with two digits.
ttThis placeholder will be replaced by either AM or PM.
:This placeholder will be replaced by the time-separator of the current culture settings.
/This placeholder will be replaced by the date-separator of the current culture settings.

The following example format string formats the date to show two day digits, two month digits, and four year digits:

Format String

MM/dd/yyyy

The following example format string formats the date-time to show two day digits, two month digits, four year digits, 2 hour digits (1 to 24), and 2 minute digits:

Format String

MM/dd/yyyy HH:mm


Was this article helpful?