String. Format () Usage

Source: Internet
Author: User
{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}

DataGrid-data format setting expression

Data format setting expression

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

{0: format specifier}

Zero 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 specifiers that can be used depend 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

Apply to this data type

Description

Price: {0: c}

Numeric/decimal

"Price:" is displayed, followed by a number in the currency format. The currency format depends on the region settings specified by the page command or the regional attribute in the web. config file.

{0: D4}

INTEGER (cannot be used with decimal places .)

An integer is displayed in the fields with four character widths filled with zero.

{0: N2} %

Numeric

The number accurate to the decimal point, followed by "% ".

{0: 000. 0}

Numeric/decimal

Number rounded to the 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 regional settings of the page or web. config file.

{0: d}

Date/datetime

Short Date Format ("12/31/99 ").

{0: yy-mm-dd}

Date/datetime

The date in the format of year-month-day (96-08-06 ).

Read-Only

When this column is in editing mode, whether the data in this column is displayed in the Editable control.

| Asp.net format -- dataformatstring

When presenting data, do not present unmodified data to users. For example, if the amount is 10 thousand RMB, If we directly display "10000", the user may be treated as one thousand or 100,000, causing the user to read data. If we make the final modification of 10 thousand yuan into "NT $10,000", it will not only make reading better, but also reduce the chance of making mistakes.
The following figure shows the completed results:
In addition to separating records by color in the DataGrid web control, the preceding data is easier to read by modifying the data of the date, unit price, and subtotal fields. To modify the output of a field, you only need to set the dataformatstring attribute of the field. The syntax is as follows:

Dataformatstring = "{0: Format String }"

We know that {0} In dataformatstring represents the data itself, while the format string after the colon represents the format that we want the data to display; in addition, after the specified format symbol, you can specify the number of digits to be displayed in decimal places. For example, if the original data is "12.34" and the format is set to {0: N1}, the output is "12.3 」. The common numeric format is shown in the following table:

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

The commonly used date formats are shown in the following table:

Format description output format
D. Simplified Date Format: mm/DD/YYYY
D detailed Date Format: dddd, Mmmm DD, yyyy
F full format (long date + short time) dddd, Mmmm DD, yyyy hh: mm
F
Complete Date and 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 and Time Format: yyyy-mm-dd hh: mm: SS
T simplified time format hh: mm
T detailed time format hh: mm: SS

String. Format

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-26t20: 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
Replace each format item in the specified string with the text equivalent item 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.