Summary of Setting data Format and String. Format in ASP. NET

Source: Internet
Author: User

Reference content is as follows:
{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
The {0: Male; female} ";" character is used to separate the positive, negative, and zero parts of the format string.

DataGrid-data format setting expression
Note the following points:
1. asp: BoundField in the GridView must set attributes when using DataFormatStringHtmlEncode = "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 must 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 formatted string as DataFormatString = "¥ {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!

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.

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 ).

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.

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

Reference content is as follows:
"{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:

Reference content is as follows:
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

Reference content is as follows:
(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:

Reference content is as follows:
Int ivisit= 100;
String szName = "Jackfled ";
Response. Write (String. Format ("your account is: {0 }. Accessed {1} Times. ", szName, iVisit ));

Related Article

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.