. NET data format settings

Source: Internet
Author: User

. Net format setting expression, which is applied to data before data is displayed in the column.
This expression is composed of optional static text and format specifiers in the following format:

{0: format specifier}

0 indicates the data element to be formatted in the column. Therefore, zero is usually used to indicate the first (and unique) element.
Format specifier is preceded by a colon (:), which consists of one or more letters indicating how to format the data.

The format specifier that can be used depends on the data type to be formatted: date, number, or other type.

The following table shows an example of formatting expressions for different data types. For more information about formatting expressions, see formatting types.

format setting expression applied to this data type description
price: {0: c} numeric/decimal display " Price: " , followed by a number in the currency format. The currency format depends on the pass page command or Web. config specify the region settings for the regional attribute in the file.
{0: d4 } integer (it cannot be used with decimals .) an integer is displayed in a zero-filled field with four character widths.
{0: n2 }% numeric display digits exactly after the decimal point, "%" .
{0: 000. 0 } numeric/decimal rounding to the number in the last decimal place. Less than three digits are filled with zero.
{0: d} date/datetime long Date Format ( " Thursday, august 06,199 6 " ). The date format depends on the page or Web. config file culture settings.
{0: d} date/datetime short date format ( " 12/31/99 " ).
{0: YY-mm-dd} date/datetime date represented by the number year-month-day ( 96-08-06 ).

Example of Setting data format in ASP. NET:
{0: d} YY-MM-DD
{0: p} % 00.00%
{0: N2} 12.68
{0: N0} 13
{0: C2 }$ 12.68
{0: d} 3/23/2003
{0: t} 12:00:00 AM
{0: Male; female}

Format -- dataformatstring of the DataGrid Data Format

Dataformatstring = "{0: Format String }"

If the original data is "12.34" and the format is set to {0: N1}, the output is "12.3 」

Format String data 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

Datetime. Now. tostring ("yyyy-mm-dd"); // The Best Mode
Aspx:
<Asp: DataGrid id = "datagrid1" runat = "server" autogeneratecolumns = "false" width = "204px">
<Columns>
<Asp: boundcolumn datafield = "date" dataformatstring = "{0: yyyy-mm-dd}"> </ASP: boundcolumn>

</ASP: DataGrid>

 When we obtain data entities from the business logic layer, the next thing is to bind them to the control. Some fields in the data object can be directly bound to the interface, but some fields need to be reformatted. For example, for the currency unit field, you need to display the currency symbol and the display separator at every three places; for example, for the date field, the database stores the date and time. However, the page is displayed in the format of xxxx, XX, and XX. In this case, the dataformatstring attribute is used.

<ASP:Gridview ID= "Grvresult" Runat= "Server" Autogeneratecolumns= "False" Width= "100%">

<Columns>

<ASP:Boundfield Headertext="Reservation date" Datafield= "Operationdate" Dataformatstring= "{0: yyyy-mm-dd }" Htmlencode= "False">

</ASP:Boundfield>

<ASP:Boundfield Headertext="Total orders" Datafield= "Totalrate" Dataformatstring= "{0: c }" Htmlencode= "False">

</ASP:Boundfield>

</Columns>

</ASP:Gridview>

For exampleCodeThe date and currency binding modes are displayed.DataformatstringIn{0}It is a fixed format, which is used with {0} In string. fromat ("{0}", somestring) to indicate the parameter index number of the bound context. Then, add a formatted string to the end. For detailed usage instructions, refer to msdn.

Note the following points:

1. In the gridviewASP:BoundfieldUseDataformatstringAttribute must be setHtmlencode= "False"Otherwise, it does not work.
2. If you want to use a formatted string of the date type, the corresponding fields in the data object should also be of the date type.
3. Format String C to indicate the currency unit. The data type to bind should be numeric. If the string type does not work, you need to manually add the format stringDataformatstring="¥{0: c }".

Summary:
Using datafromatstring in the gridview is somewhat different from using it in the DataGrid! The htmlencode attribute is added to boundfield in the gridview and the default value is true, which invalidates datafromatstring!

String. FormatFormat result

String. Format

(C) Currency:... ($123.00)

(D) decimal: ......-123

(E) Scientific:...-1.234500e + 002

(F) Fixed Point:...-123.45

(G) General:...-123

(N) number:...-123.00

(P) percent: ......-12,345.00%

(R) round-trip: .........-123.45

(X) hexadecimal: ...... ffffff85

(D) Short Date :....... 6/26/2004

(D) Long Date:... Saturday, June 26,200 4

(T) Short Time :...

(T) long time: ...... 8:11:04

(F) Full date/short time: .. Saturday, June 26,200 4 pm

(F) Full date/long time:... Saturday, June 26,200 4 8:11:04

(G) General Date/short time:. 6/26/2004 8: 11 pm

(G) General Date/long time:. 6/26/2004 8:11:04

(M) month :...

(R) rfc1123:... sat, 26 Jun 2004 20:11:04 GMT

(S) sortable :........ 2004-06-26 T20: 11: 04

(U) Universal sortable:... 2004-06-26 20: 11: 04z (invariant)

(U) Universal sortable:... Sunday, June 27,200 4 3:11:04 AM

(Y) year:... June, 2004

(G) General:... green

(F) flags: ...... Green (flags or integer)

(D) decimal number:... 3

(X) hexadecimal: ....... 00000003

 

 

Note:
String. Format
StringReplace each format item with the text equivalent of the value of the corresponding object.

Example:

Int ivisit= 100;
String szname = "jackfled ";
Response. Write (string. Format ("Your account is:{0}. Accessed{1}Times. ", Szname, ivisit ));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.