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 tostringToFormat
.dataFormat
(string): The data format of the string. Accepted values:Date
orNumber
.cultureCode
(string): A culture code to apply to the string.
Use these codes to format strings as numeric values.
Code | Output Format |
---|---|
C | Currency |
D | Decimal |
E | Scientific notation |
F | Fixed-point |
G | General |
N | Number |
P | Percent |
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: 2024) | Month (Example: August) | Date (Example: Monday, August 5) | Hours (Example: 20) | Minutes (Example: :00) | Seconds (Example: :05) | AM/PM (Example: PM) | Offset (Example: -6:00) |
---|---|---|---|---|---|---|---|
y = 24 | M = 8 | d = 5 | h = 8 | m = 0 | s = 5 | t = P | z = -6 |
yy = 24 | MM = 08 | dd = 05 | hh = 08 | mm = 00 | ss = 05 | tt = PM | zz = -06 |
yyy = 2024 | MMM = Aug | ddd = Mon | H = 8 | - | - | - | zzz = -06:00 |
- | MMMM = August | dddd = Monday | HH = 20 | - | - | - | - |
This example shows how to format the date to include the full name of the day ("dddd"), 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, seconds, and UTC offset ("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.
- NumberFormatInfoClass Class on the Microsoft Developer Network
- DateTimeFormatInfo Class on the Microsoft Developer Network