Number, Currency format:
After the specified format symbol, you can specify the number of digits to display for the decimal. For example, the original data is "1.56", if the format is set to {0:n1}, then the output is "1.5". Its commonly used numerical formats are shown in the following table:
format string input result
' {0:c} ' 12345.6789 $12,345.68
' {0:c} '-12345.6789 ($12,345.68)
"{0:d}" 12345 12345
"{0:d8}" 12345 00012345
' {0:e} ' 12345.6789 1234568E+004
' {0:e10} ' 12345.6789 1.2345678900E+004
"{0:f}" 12345.6789 12345.68
"{0:f0}" 12345.6789 12346
"{0:g}" 12345.6789 12345.6789
' {0:g7} ' 123456789 1.234568E8
' {0:n} ' 12345.6789 12,345.68
' {0:n4} ' 123456789 123,456,789.0000
Total: {0:c} ' 12345.6789 total: $12345.68
Popular date-time formats:
Format Description Output format
D Compact Date format mm/dd/yyyy
D Detail Date format dddd, MMMM DD, yyyy
F Full Format (long date + short time) dddd, MMMM dd, yyyy hh:mm
F
Full Date Time format
(Long date + long time)
dddd, MMMM dd, yyyy HH:mm:ss
G General format (short date + short time) mm/dd/yyyy hh:mm
G General Format (short date + long time) mm/dd/yyyy HH:mm:ss
M,m month-day format MMMM DD
s moderate date time format Yyyy-mm-dd HH:MM:SS
T streamlined time format hh:mm
T Verbose time Format HH:mm:ss
Public virtual stringdataformatstring {get; set;} |
C++ |
Public:
Virtual property string^ dataformatstring {
string^ get ();
Voidset (string^ value);
} |
J # |
/** @property * * * public
stringget_dataformatstring ()
/** @property/public
voidset_dataformatstring ( StringValue) |
Jscript |
Public function Get DataFormatString (): stringpublic function set dataformatstring (value:string) |
Property Value
A formatted string that specifies the display format for the field value. The default value is an empty string (""), which indicates that no special formatting has been applied to the field value. Notes
Use the DataFormatString property to specify a custom display format for the values displayed in the BoundField object. If the DataFormatString property is not set, the value of the field does not use any special formatting when it is displayed. By default, a formatted string is applied to a field value only if the data-bound control containing the BoundField object is in read-only mode. To apply a formatted string to a field value in edit mode, set the Applyformatineditmode property to True.
The formatted string can be any string, and typically contains placeholders for field values. For example, in the format string "Item Value: {0}", when a string is displayed in the BoundField object, the value of the field replaces the {0} placeholder. The remainder of the formatted string is displayed as text.
Attention |
If the formatted string does not contain a placeholder, the field values from the data source are not included in the final display text. |
Placeholders are composed of two parts separated by colons and enclosed in braces, in the form of {a:bxx}. The value before the colon (a in the general example) specifies the index of the field value in the zero-based argument list.
Attention |
This parameter is part of the formatting syntax. Because there is only one field value per cell, this value can only be set to 0. |
The colon and the value following the colon are optional. The character after the colon (B in the general example) specifies the display format for the value. The following table lists some common formats.
Format characters |
Description |
C |
Displays the value in currency format. |
D |
Displays numeric values in decimal format. |
E |
Displays values in scientific notation (exponential) format. |
F |
Displays the values in a fixed format. |
G |
Displays the values in a regular format. |
N |
Displays numeric values in numeric format. |
X |
Displays a numeric value in hexadecimal format. |
Attention |
Format characters are case-insensitive, except for X, which displays hexadecimal characters in the case specified. |
The value after the format character (xx in the general example) specifies the number of significant digits or decimal places for the displayed value. For example, the format string "{0:F2}" displays a fixed-point number with a two-bit decimal.
For more information about formatting strings, see formatting overviews. <%@ Page language= "C #"%>
<body>
<form runat= "Server" >
<asp:gridview id= "Discountsgridview"
Datasourceid= "Discountssqldatasource"
Autogeneratecolumns= "False"
runat= "Server" >
<columns>
<asp:boundfield datafield= "Discounttype"
headertext= "Discount Type"/>
<asp:boundfield datafield= "Discount"
Dataformatstring= "... {0:f4}% "
Itemstyle-horizontalalign= "Right"
headertext= "Discount"/>
</columns>
</asp:gridview>
<!--This example uses Microsoft SQL Server and connects-->
<!--to the Pubs sample database. -->
<asp:sqldatasource id= "Discountssqldatasource"
Selectcommand= "SELECT [Discounttype], [stor_id], [Lowqty], [Highqty], [discount] from [discounts]"
connectionstring= "<%$ connectionstrings:pubsconnectionstring%>"
runat= "Server" >
</asp:sqldatasource>
</form>
</body>