"Reprint of C # String.Format Example"

Source: Internet
Author: User

Examples of 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
  • Stringstr12 = (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 type
  • 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
  • G or G
  • Conventional
  • 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);
  • /******************************************************************************/
  • Asp. NET Set data format and String.Format Usage Summary (citation)
  • {0:D} Yy-mm-dd
  • {0:p} percent 0%
  • {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 Settings expression
  • Data format Settings expressions
  • . NET Framework formatting expressions, which are applied to data before the data is displayed in the column. This expression consists of optional static text and a format specifier, which is represented in the following format:
  • {0:format specifier}
  • 0 is the parameter index, which indicates the data element to be formatted in the column; therefore, the first (and only) element is usually indicated with zero. The format specifier is preceded by a colon (:), which consists of one or more letters that indicate how the data is formatted. The format specifier you can use depends on the type of data you want to format: date, number, or other type. The following table shows examples of formatting expressions for different data types. For more information about formatting expressions, see Formatting types.
  • Formatting an expression
  • Apply to this data type
  • Description
  • Price: {0:C}
  • numeric/Decimal
  • Displays "Price:" followed by a number in currency format. The currency format depends on the culture settings specified through the Page directive or the Culture property in the Web. config file.
  • {0:D4}
  • Integer (cannot be used with decimals.) )
  • An integer is displayed in a field filled with four characters justifies by 0.
  • {0:n2}%
  • Numeric
  • Displays a number that is exactly two digits after the decimal point, followed by "%".
  • {0:000.0}
  • numeric/Decimal
  • Rounded to one digit after the decimal point. Numbers less than three digits are populated with 0.
  • {0:D}
  • Date/datetime
  • Long date format ("Thursday, August 06, 1996"). The date format depends on the culture settings of the page or Web. config file.
  • {0:D}
  • Date/datetime
  • Short Date format ("12/31/99").
  • {0:YY-MM-DD}
  • Date/datetime
  • Date represented by the number of years-months-days (96-08-06).
  • Read-only
  • When this column is in edit mode, the data in the column is displayed in an editable control.
  • 2006-02-22 | format--dataformatstring for ASP. NET Data format
  • When we present the data, we do not present the unmodified data to the consumer. For example, the amount of $10,000, if we directly display "10000", may lead to users as 1000 or 100,000, causing users to read the data problems. If we finish the 10,000-yuan retouching after the output of "nt$10,000", not only make it better to read, but also let users reduce the chance of making mistakes.
  • The following images are the result of retouching:
  • The above data, in addition to the DataGrid Web control in color to separate records, the most important is the date, unit price, and subtotal, the data of the three-meter field is easier to read. To modify the output of a field, simply set the DataFormatString property of the field, using the following syntax:
  • dataformatstring="{0: format string}"
  • We know that {0} in dataformatstring represents the data itself, whereas the format string after the colon represents the format that you want the data to display, and you can specify the number of digits that the decimal will display after the specified format symbol. For example, the original data is "12.34", and if the format is set to {0:n1}, the output is "12.3". Its commonly used numeric format is shown in the following table:
  • format string Data results
  • "{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
  • Its commonly used date format is shown in the following table:
  • Format Description Output format
  • D Thin 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 Time format
  • (long Date + long time)
  • dddd, MMMM dd, yyyy HH:mm:ss
  • G General format (shortdate + short time ) mm/dd/yyyy hh:mm
  • G General format (shortdate + long time) mm/dd/yyyy HH:mm:ss
  • M,m month-day format MMMM DD
  • s medium Date time format Yyyy-mm-dd HH:MM:SS
  • T Lite time Format hh:mm
  • T Verbose time Format HH:mm:ss
  • String.Format format Results
  • 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, 2004
  • (t) short time: ..... 8:11 PM
  • (T) Long time: ..... 8:11:04 PM
  • (f) Full date/short time:. Saturday, June, 2004 8:11 PM
  • (F) Full date/long time: ... Saturday, June, 2004 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, June 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, 2004 3:11:04 AM
  • (Y) Year: ..... June, 2004
  • (G) General: ..... . Green
  • (F) Flags: ..... Green (Flags or integer)
  • (D) Decimal number: ..... 3
  • (X) Hexadecimal: ..... 00000003
  • Description
  • String.Format
  • Replaces each format item in the specified String with the text equivalent of the value of the corresponding object.
  • Example:
  • int ivisit = 100;
  • String szName = "jackfled";
  • Response.Write (String.Format ("Your account number is: {0}.")  visited {1} times. ", SzName, Ivisit));

"Reprint of C # String.Format Example"

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.