Вы находитесь на странице: 1из 5

Note In most cases, formatting depends on the server's culture setting.

The examples are for a culture setting of en-US. Format Description Example character Format: {0:C} Displays numeric values in currency format. You can specify the number of decimal places. 123.456 -> $123.46 Format: {0:C3} 123.456 -> $123.456 Format: {0:D} Displays integer values in decimal format. You can 1234 -> 1234 specify the number of digits. (Although the type is referred to as "decimal", the numbers are formatted as Format: {0:D6} integers.) 1234 -> 001234 Format: {0:E} 1052.0329112756 -> 1.052033E+003 Format: {0:E2} -1052.0329112756 -> 1.05e+003 Format: {0:F} Displays numeric values in fixed format. You can specify the number of decimal places. 1234.567 -> 1234.57 Format: {0:F3} 1234.567 -> 1234.567 Format: {0:G} Displays numeric values in general format (the most compact of either fixed-point or scientific notation). You can specify the number of significant digits. -123.456 -> -123.456 Format: {0:G2}

C or c

D or d

E or e

Displays numeric values in scientific (exponential) format. You can specify the number of decimal places.

F or f

G or g

N or n

-123.456 -> -120 Displays numeric values in number format (including Format: {0:N} group separators and optional negative sign). You can

specify the number of decimal places.

1234.567 -> 1,234.57 Format: {0:N4} 1234.567 -> 1,234.5670 Format: {0:P}

P or p

Displays numeric values in percent format. You can specify the number of decimal places.

1 -> 100.00% Format: {0:P1} .5 -> 50.0% Format: {0:R}

R or r

Displays Single, Double, or BigInteger values in round-trip format.

123456789.12345678 -> 123456789.12345678 Format: {0:X} 255 -> FF Format: {0:x4} 255 -> 00ff

X or x

Displays integer values in hexadecimal format. You can specify the number of digits.

For more information and for examples that show formatting for other culture values, see Standard Numeric Format Strings. You can also create custom numeric format strings. For more information, see Custom Numeric Format Strings. The following table lists format characters for DateTime fields. Most of these formatting specifications result in a different output depending on culture settings. The examples are for a DateTime value of 6/15/2009 1:45:30 PM with a culture setting of en-US. Format character d Short date pattern. Description Example Format: {0:d} 6/15/2009 1:45:30 PM -> 6/15/2009 Format: {0:D} 6/15/2009 1:45:30 PM >Monday, June 15, 2009 Format: {0:f}

Long date pattern.

Full date/time pattern (short time).

6/15/2009 1:45:30 PM -> Monday, June 15, 2009 1:45 PM Format: {0:F} F Full date/time pattern (long time). 6/15/2009 1:45:30 PM -> Monday, June 15, 2009 1:45:30 PM Format: {0:g} 6/15/2009 1:45:30 PM -> 6/15/2009 1:45 PM Format: {0:G} 6/15/2009 1:45:30 PM -> 6/15/2009 1:45:30 PM Format: {0:M} 6/15/2009 1:45:30 PM -> June 15 Format: {0:o} 6/15/2009 1:45:30 PM -> 200906-15T13:45:30.0900000 Format: {0:R} 6/15/2009 1:45:30 PM -> Mon, 15 Jun 2009 20:45:30 GMT Format: {0:s} 6/15/2009 1:45:30 PM -> 200906-15T13:45:30 Format: {0:t} 6/15/2009 1:45:30 PM -> 1:45 PM Format: {0:T} 6/15/2009 1:45:30 PM -> 1:45:30 PM Format: {0:u} 6/15/2009 1:45:30 PM -> 200906-15 20:45:30Z

General date/time pattern (short time).

General date/time pattern (long time).

M or m

Month/day pattern.

O or o

Round-trip date/time pattern.

R or r

RFC1123 pattern (for information, see DateTimeFormatInfo.RFC1123Pattern).

Sortable date/time pattern.

Short time pattern.

Long time pattern.

Universal sortable date/time pattern.

Format: {0:U} U Universal full date/time pattern. 6/15/2009 1:45:30 PM -> Monday, June 15, 2009 8:45:30 PM Format: {0:Y} 6/15/2009 1:45:30 PM -> June, 2009

Y or y

Year month pattern.

For more information and for examples that show formatting for other culture values, see Standard Date and Time Format Strings. You can also create custom date and time format strings. For more information, see Custom Date and Time Format Strings. Examples

A Visual Studio Web site project with source code is available to accompany this topic: Download. The following example shows how to use the DataFormatString property to specify a custom display format for the values of a field. None
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AWLTConnectionString %>" SelectCommand="SELECT ProductID, Name, ProductNumber, ListPrice, Weight, ModifiedDate FROM SalesLT.Product"> </asp:SqlDataSource> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False" ReadOnly="True" SortExpression="ProductID" DataFormatString="{0:D6}" />

<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" DataFormatString="{0}" /> <asp:BoundField DataField="ProductNumber" HeaderText="ProductNumber" SortExpression="ProductNumber" DataFormatString= "#{0}" /> <asp:BoundField DataField="ListPrice" HeaderText="ListPrice" SortExpression="ListPrice" DataFormatString="{0:C}" /> <asp:BoundField DataField="Weight" HeaderText="Weight" SortExpression="Weight" DataFormatString="{0:F3}" /> <asp:BoundField DataField="ModifiedDate" HeaderText="ModifiedDate" SortExpression="ModifiedDate" DataFormatString="{0:d}" /> </Columns> </asp:GridView> </div> </form> </body> </html>

The following example shows one row of the output that is produced by this example. ProductID Name ProductNumber ListPrice Weight ModifiedDate 000680 HL Road Frame - Black, 58 #FR-R92B-58 $1,431.50 1016.040 3/11/2004