Log In
Home
Support
Demos
Documentation
Blogs
Training
Webinars
[Expand]General Information
[Collapse]WinForms Controls
  Prerequisites
 [Expand]What's Installed
 [Expand]Build an Application
 [Expand]Controls and Libraries
 [Collapse]Common Features
  [Expand]Data Binding Common Concepts
  [Expand]Data Source Wizard
  [Expand]Expressions
  [Expand]Behaviors
  [Expand]Application Appearance
  [Expand]Filtering UI Context
  [Expand]High DPI Support
  [Expand]Scaffolding Wizard
  [Collapse]Formatting Values
    How to: Format Date-Time Values Using the LongDate Pattern
    How to: Format Date-Time Values Using a Custom Format Pattern
    How to: Format a Numeric Value according to its Sign
    How to: Add Custom Text to a Formatted String
    How to: Create a Custom Formatter to Change the Case of String Values
    How to: Create a Custom Formatter to Represent Decimal Values in Binary
    Formatting Approaches
    Members that Support the Formatting Mechanism
    Format Specifiers
    Composite Formatting
    Custom Formatting
   HTML Text Formatting
  [Expand]Menus
  [Expand]Tooltip Management
  [Expand]Saving and Restoring Layouts
   Clipboard - Copy and Paste Operations. Data Formatting
   Version Compatibility: Default Property Values
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Document Server
[Expand]Reporting
[Expand]Report Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

Format Specifiers

Format specifiers are symbols that specify the way data is formatted during its conversion to a string. This topic describes frequently used standard and custom format specifiers used to format numeric and date/time values. For a complete list, please refer to the Format Specifiers topic in MSDN.

To add custom text to the output string, you may choose between custom format specifiers (when formatting numeric or date/time values) and the composite formatting feature (when formatting any value).

Expanded General

If numeric values need to be formatted, set the FormatInfo.FormatType property to FormatType.Numeric. To format date/time values, set this property to FormatType.DateTime. Then, use the FormatInfo.FormatString property to specify a standard or custom format string.

Expanded Standard Format Strings for Numeric Values

Standard format strings for numeric values are specified in the Axx format. Here A is a character called the format specifier. xx is a sequence of optional digits called the precision specifier. The format specifier denotes whether or not values should be transformed into a currency format, scientific notation, etc. This specifier must be set to one of the predefined characters listed in the Standard Numeric Format Strings topic in MSDN. The following table from the MSDN article Standard Numeric Format Strings gives several examples.

Format Specifier Description Sample Format String Sample Output
c or C Converts the given number into a string that represents a currency amount. The precision specifier indicates the desired number of decimal places. If the precision specifier is omitted, the default currency precision from the current regional options is used. c2 $1,234.00
e or E Converts the given number into a string of the "-d.ddd...E+ddd" or "-d.ddd...e+ddd" form, in which each 'd' indicates a digit (0-9). The string begins with a minus sign if the number is negative. One digit always precedes the decimal point. The precision specifier indicates the desired number of digits after the decimal point. If the precision specifier is omitted, a default of six digits after the decimal point will be used. The case of the format specifier indicates whether to prefix the exponent with an 'E' or an 'e'. The exponent always consists of a plus or minus sign, and a minimum of three digits. The exponent is padded with zeros to meet this minimum if required. E1 1.2E+003
n or N Converts the given number into a string of the "-d,ddd,ddd.ddd..." form, in which each 'd' indicates a digit (0-9). The string begins with a minus sign if the number is negative. Thousand separators are inserted between each group of three digits to the left of the decimal point. The precision specifier indicates the desired number of decimal places. If the precision specifier is omitted, the default currency precision from the current regional options is used. n0 1,234
x or X Converts the given number into a string of hexadecimal digits. The case of the format specifier indicates whether uppercase or lowercase characters are used for hexadecimal digits greater than 9. The precision specifier indicates the minimum number of digits in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier. This format is supported for integral types only. X8 000004D2

Expanded Standard Format Strings for Date/Time Values

Standard date and time format strings contain a single character. This character defines the pattern used to represent the value (if and how to display year numbers, month numbers, etc.). All specifiers are covered in the Standard Date and Time Format Strings topic in MSDN. The following table from the MSDN article Standard Date and Time Format Strings provides a few examples.

Format Specifier Description Sample Output
d Short date pattern 3/12/2003
D Long date pattern Wednesday, March 12, 2003
t Short time pattern 12:00 AM
T Long time pattern 12:00:00 AM
f Full date/time pattern (short time) Wednesday, March 12, 2003 12:00 AM
F Full date/time pattern (full time) Wednesday, March 12, 2003 12:00:00 AM
g General date/time pattern (short time) 3/12/2003 12:00 AM
G General date/time pattern (full time) 3/12/2003 12:00:00 AM

Expanded Example 1

The following code demonstrates a way of formatting values in a DateEdit control using the Long Date pattern (when editing is disabled). The RepositoryItemDateEdit.DisplayFormat property is customized to implement this formatting type. See the Format Specifiers topic for information on format specifiers.

The result for the English (USA) culture is displayed below.

Expanded Standard Format Strings for TimeSpanEdit Values

Standard TimeSpanEdit format strings contain a single character, which defines the format specifier used to represent the value (if and how to display year numbers, month numbers, etc.).

The following table lists the standard date time format specifiers used for the TimeSpanEdit control.

Format Specifier Description Sample Output
d and D

Date/time format. It is not culture-sensitive. The t and T, f and F format specifiers' behavior is identical to the d.

Formats take the form [-][d.]hh:mm:ss

1.5:25:30
g General short format. This culture-sensitive format outputs the compact form of a TimeSpanEdit value.

The format has the form [-][d:]h:mm:ss

1:5:25:30
G General long format. This culture-sensitive format outputs the long form of a TimeSpanEdit value (days and seven fractional digits).

The format takes the form [-]d:hh:mm:ss.fffffff

1:05:25:30.0000000

Expanded Custom Format Strings for Numeric Values

Custom format strings are used to construct format patterns manually. You only need to use custom formatting when the standard format strings do not meet your requirements. All format strings represented by a literal character, followed by one or two digits, are treated as standard format strings, and thus all other strings are interpreted as custom format strings. The table below lists the most frequently used characters that can construct a custom format string. Please refer to the Custom Numeric Format Strings topic in MSDN for a complete list of available characters.

Character Meaning
0 The digit is always displayed.
# The digit is displayed only when needed (i.e., used to suppress leading zeros).
. Specifies the position of the decimal point. The appearance of the point depends on regional settings.
, Specifies the position of a comma. The appearance of the comma depends on regional settings.

Note that custom format strings may also contain other characters that will be copied to the formatted string. This can be used to add explanatory text to the value. If you need to display one of the reserved characters, it must be preceded by the '\' symbol.

When formatting numeric values, you can apply different formats to positive, negative and zero values. To do this, the format string must contain three parts delimited by semicolons. The first part sets the positive values format, the second is applied to negative values and the third represents zero values.

Expanded Example 2

The following example demonstrates a way of representing values in a CalcEdit editor that is dependent on the sign of the values. This type of formatting is implemented using custom format specifiers. Formatting is applied via the editor's RepositoryItem.DisplayFormat property, so this affects the text in display mode only and is not in effect in edit mode. See the Format Specifiers topic for information on format specifiers.

The sample code execution result is shown in the image below.

Expanded Custom Format Strings for Date/Time Values

To create format patterns for date and time values, combine the strings listed in the tables below. These strings represent the year, month, day, number and so on, in different formats.

The following table lists the most frequently used strings that can be used to format dates.

Symbol Meaning Result of formatting a sample value (9/2/2003)
yy The last two digits of the year. 03
yyyy A four digit year. 2003
MM The number of the month. 09
MMM A short text description of the month. Sep
MMMM The full name of the month. September
dd The number of the day. 02
ddd A short text for the day of the week. Tue
dddd The full name of the day of the week. Tuesday
/ Date separator. Its appearance depends on regional settings.  

The next table lists strings that are used to format time values.

Symbol Meaning
hh Hours
mm Minutes
ss Seconds
tt If present, represents data in AM/PM format
: Time separator; its appearance depends upon regional settings
Note

The tables above lists only the most frequently used format string portions. For a complete list, please refer to the Custom Date and Time Format Strings topic in MSDN.

Expanded Example 3

The following sample code demonstrates a way of formatting date/time values in a DateEdit control using a custom format string. See the Format Specifiers topic for information on format specifiers.

Expanded Custom Format Strings for TimeSpanEdit Values

To create a custom format string for TimeSpanEdit values, combine the strings listed in the tables below. These strings represent the custom date and time format specifiers.

The complete list of specifiers is in the Custom TimeSpan Format Strings topic in MSDN.

The following table lists the most frequently used strings that can be used to format date and time values.

Format specifier Description Result of formatting a sample value
(1 day, 5 hours, 25 minutes, 30 seconds)
d, %d The number of days in the interval. 1
dd-dddddddd The number of days with leading zeros for single- or double-digit days. 01-00000001
h, %h The number of whole hours that are not included as part of days without a leading zero. 5
hh The number of whole hours that are not included as part of days with a leading zero. 05
m, %m The number of whole minutes that are not included as part of hours without a leading zero. 25
mm The number of whole minutes that are not included as part of hours with a leading zero. 25
s, %s The number of whole seconds that are not included as part of minutes without a leading zero. 30
ss The number of whole seconds that are not included as part of minutes with a leading zero. 30

The following sample code illustrates how to format the specified time span value in a TimeSpanEdit control using a custom format string.

How would you rate this topic?​​​​​​​