Format()

Formats a string in the specified format.

The Format() function has four parameters:

  • stringToFormat (string): Required. The string to apply formatting rules to.
  • outputFormat (string): Required. A C# compatible format string to apply to stringToFormat.
  • dataFormat (string): The data format of the string. Accepted values: Date or Number.
  • cultureCode (string): A culture code to apply to the string.

You can use the following codes to format strings.

CodeOutput Format
CCurrency
DDecimal
EScientific Notation
FFixed-point
GGeneral
NNumber
PPercent

You can also format strings as dates. See Formatting Dates for more information.

You can use this function to format strings in several different ways. These examples show some of the ways that you can use this function.

You can format a date string using a custom format by passing a string that specifies the format. This table shows the formatting elements that you can pass in a custom date string.

Year (Example: 2011)Month (Example: August)Date (Example: Saturday, August 06)Hours (Example: 20)Minutes (Example: :00)Seconds (Example: :05)AM/PM (Example: PM)Offset (Example: -6:00)
y = 11M = 8d = 6h = 8m = 0s = 5t = Pz = -6
yy = 11MM = 08dd = 06hh = 08mm = 00ss = 05tt = PMzz = -06
yyy = 2011MMM = Augdddd = SatH = 8---zzz = -06:00
yyyy = 2011MMMM = Augustddddd = SaturdayHH = 20----

This example shows how to format the date to include the full name of the day ("ddddd"), the full name of the month ("MMMM"), the date without a leading zero ("d"), and the year ("yyyy"). It also shows the time in 24-hour format, including hours, minutes, and seconds ("h:mm").

The function outputs the date and time using the format you specified.

You can use the cultureCode parameter to express day and month names in other languages. This example outputs the date using day and month names in Swahili.

The function outputs the date string with the appropriate values translated.

The function outputs the string expressed as a dollar amount rounded to two decimal places.

You can use the cultureCode parameter to express currency values for a different country. This example outputs the currency value in Indian Rupees.

The function outputs the currency value expressed in Indian rupees, rounded to the nearest whole number.