# Format Specifiers - Quick Reference

### Numeric Format Specifiers

The full list of format specifiers and the effect of the precision specifier for each is as follows:

Specifier | Description | Effect of Precision Specifier |
---|---|---|

C or c | Formats the number as a monetary value including the correct number of decimal places and the appropriate currency symbol for the user's local setting | Specifies a fixed number of decimal places. |

D or d | Formats integers only as simple whole numbers. | Specifies the minimum number of digits. Leading zeroes are added if required. |

E | Formats numbers using exponential notation. The resultant string includes an upper case letter 'E'. | Specifies a fixed number of decimal places for the mantissa. If omitted, six decimal places are used. |

e | Formats numbers using exponential notation. The resultant string includes a lower case letter 'e'. | Specifies a fixed number of decimal places for the mantissa. If omitted, six decimal places are used. |

F or f | Formats numbers using fixed-point notation. | Specifies a fixed number of decimal places. |

G or g | Formats numbers using either exponential or fixed-point notation, whichever produces the shortest string. The actual results vary according to the data type being converted and whether a precision specifier is used. | See 'e', 'E' and 'F or f'. |

N or n | Formats numbers using fixed-point notation with thousands separators. | Specifies a fixed number of decimal places. |

P or p | Formats numbers using percentage notation. For example, 0.25 is formatted as 25%. | Specifies a fixed number of decimal places. |

R or r | Formats numbers using Round-Trip Format. This is a special format that ensures that the string generated can be converted back to the original number using Parse methods. |
Unused. |

X | Converts numbers into a string representation of their hexadecimal value. The digits 0 to 9 and A to F are used. | Specifies the minimum number of digits. Leading zeroes are added if required. |

x | Converts numbers into a string representation of their hexadecimal value. The digits 0 to 9 and a to f are used. | Specifies the minimum number of digits. Leading zeroes are added if required. |

## Numeric Picture Formats for Custom Formatting

The standard format specifiers are useful in most situations. However, there may be circumstances where the format specifiers do not provide the desired results. In these cases you can use a *picture format*. A picture format is a string containing special characters that affect the final conversion. There are a number of control characters; each will be considered in the following sections. For more details check this post http://www.blackwasp.co.uk/CSharpNumericToString_2.asp

### DateTime Format Specifiers

The full list of format specifiers for DateTime conversion is as follows:

Specifier | Description | Example |
---|---|---|

d | Short date format. This is equivalent to using ToShortDateString. | "03/01/2007" |

D | Long date format. This is equivalent to using ToLongDateString. | "03 January 2007" |

f | Date and time using long date and short time format. | "03 January 2007 21:25" |

F | Date and time using long date and time format. | "03 January 2007 21:25:30" |

g | Date and time using short date and time format. | "03/01/2007 21:25" |

G | Date and time using short date and long time format. | "03/01/2007 21:25:30" |

m | Day and month only. | "03 January" |

r | Date and time in standard Greenwich Mean Time (GMT) format. | "Wed, 03 Jan 2007 21:25:30 GMT" |

s | Sortable date and time format. The date elements start at the highest magnitude (year) and reduce along the string to the smallest magnitude (seconds). | "2007-01-03T21:25:30" |

t | Short time format. This is equivalent to using ToShortTimeString. | "21:25" |

T | Long time format. This is equivalent to using ToLongTimeString. | "21:25:30" |

u | Short format, sortable co-ordinated universal time. | "2007-01-03 21:25:30Z" |

U | Long format date and time. | "03 January 2007 17:25:30" |

y | Month and year only. | "January 2007" |

### DateTime Picture Formatting Codes

The standard format specifiers are useful in most cases. On some occasions however, it is necessary to have complete control over the positioning and styling of each element of date and time information. For this, you can use *picture formats*, allowing an exact format to be created using any combination of elements from a DateTime

The above example shows several formats and the results. The full list of formatting codes is as follows:

Specifier | Description | Examples |
---|---|---|

y | One-digit year. If the year cannot be specified in one digit then two digits are used automatically. | "7" "95" |

yy | Two-digit year with leading zeroes if required. | "07" |

yyyy | Full four-digit year. | "2007" |

g or gg | Indicator of Anno Domini (AD). | "A.D." |

M | One-digit month number. If the month cannot be specified in one digit then two digits are used automatically. | "1" "12" |

MM | Two-digit month number with leading zeroes if required. | "01" |

MMM | Three letter month abbreviation. | "Jan" |

MMMM | Month name. | "January" |

d | One-digit day number. If the day cannot be specified in one digit then two digits are used automatically. | "3" "31" |

dd | Two-digit day number with leading zeroes if required. | "03" |

ddd | Three letter day name abbreviation. | "Wed" |

dddd | Day name. | "Wednesday" |

h | One-digit hour using the twelve hour clock. If the hour cannot be specified in one digit then two digits are used automatically. | "9" "12" |

hh | Two-digit hour using the twelve hour clock with leading zeroes if required. | "09" |

H | One-digit hour using the twenty four hour clock. If the hour cannot be specified in one digit then two digits are used automatically. | "1" "21" |

HH | Two-digit hour using the twenty four hour clock with leading zeroes if required. | "09" |

t | Single letter indicator of AM or PM, generally for use with twelve hour clock values. | "A" "P" |

tt | Two letter indicator of AM or PM, generally for use with twelve hour clock values. | "AM" "PM" |

m | One-digit minute. If the minute cannot be specified in one digit then two digits are used automatically. | "1" "15" |

mm | Two-digit minute with leading zeroes if required. | "01" |

s | One-digit second. If the second cannot be specified in one digit then two digits are used automatically. | "1" "59" |

ss | Two-digit second with leading zeroes if required. | "01" |

f | Fraction of a second. Up to seven f's can be included to determine the number of decimal places to display. | "0" "0000000" |

z | One-digit time zone offset indicating the difference in hours between local time and UTC time. If the offset cannot be specified in one digit then two digits are used automatically. | "+6" "-1" |

zz | Two-digit time zone offset indicating the difference in hours between local time and UTC time with leading zeroes if required. | "+06" |

zzz | Full time zone offset indicating the difference in hours and minutes between local time and UTC time with leading zeroes if required. | "+06:00" |

Some of the formatting codes use the same letter as a format specifier. When used within a format string that is greater than one character in length this does not present a problem. If you need to use a single character picture format string, the letter must be preceded by a percentage sign (%) to indicate that the standard format specifier is not to be used.

### Guids Format Specifiers

The four available format specifiers are listed in the table below.

Specifier | Description | Examples |
---|---|---|

D | Default format of thirty-two digits with groups separated by hyphens. | "00000000-0000-0000-0000-000000000000" |

N | A series of thirty-two hexadecimal digits with no separators between groups. | "00000000000000000000000000000000" |

B | Default format surrounded by brackets. | "{00000000-0000-0000-0000-000000000000}" |

P | Default format surrounded by parentheses. | "(00000000-0000-0000-0000-000000000000)" |