C # various string formats,

Source: Internet
Author: User

C # various string formats,

C # String. format: stringstr1 = string. format ("{0: N1}", 56789); // result: 56,789.0 stringstr2 = string. format ("{0: N2}", 56789); // result: 56,789.00 stringstr3 = string. format ("{0: N3}", 56789); // result: 56,789.000 stringstr8 = string. format ("{0: F1}", 56789); // result: 56789.0 stringstr9 = string. format ("{0: F2}", 56789); // result: 56789.00 stringstr11 = (56789/100 .0 ). toString ("#. # "); // result: 567.89 string Str12 = (56789/100 ). toString ("#. # "); // result: 567 C or c currency Console. write ("{0: C}", 2.5); // $2.50 Console. write ("{0: C}",-2.5); // ($2.50) D or d decimal number Console. write ("{0: D5}", 25); // 00025 E or e scientific Console. write ("{0: E}", 250000); // 2.500000E + 005 F or f Fixed Point Console. write ("{0: F2}", 25); // 25.00 Console. write ("{0: F0}", 25); // 25 GB or G standard Console. write ("{0: G}", 2.5); // 2.5 N or n digital Console. write ("{0: N }", 2500000); // 2,500,000.00 X or x hexadecimal Console. write ("{0: X}", 250 ); /*************************************** ***************************************/ /* Ask hovertree.com */ASP. NET sets the data format and String. format Use Summary (reference) {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.. NET Framework 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 is a parameter index, which indicates the data element to be formatted in the column; therefore, generally, zero is 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. The format setting expression applies to this data type description Price: {0: C} numeric/decimal displays "Price:", 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 precise to the two digits after the decimal point, followed by "% ". {0: 000. 0} numeric/decimal rounding to the digit after the decimal point. 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 indicates the date (96-08-06) by year-month-Day of the number ). Read-Only: When this column is in editing mode, whether the data in this column is displayed in the Editable control. | Format-DataFormatString of asp.net data Format. 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 polishing result: In addition to separating the DataGrid Web control by color, it is easier to modify 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 be displayed; 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 commonly used 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} "12 345.6789 Total: $12345.68 common date formats: 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 full date 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 Format yyyy-MM-dd HH: mm: ss t simplified time format HH: mm T detailed time format HH: Mm: ss string. format 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 d Ate :....... 6/26/2004 (D) Long date :........ saturday, June 26,200 4 (t) Short time :....... PM (T) Long time :........ 8:11:04 PM (f) Full date/short time :.. saturday, June 26,200 4 PM (F) Full date/long time :... saturday, June 26,200 4 8:11:04 PM (g) General date/short time :. 6/26/2004 8: 11 PM (G) General date/long time :. 6/26/2004 8:11:04 PM (M) Month :. ......... June 26 (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) De Cimal number :..... 3 (X) Hexadecimal :....... 00000003 Description: String. format replaces 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 ));

 

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.