examples of C # String.Format STRINGSTR1=string. Format ("{0:N1}",56789);//result:56,789.0STRINGSTR2 =string. Format ("{0:N2}",56789);//result:56,789.00STRINGSTR3 =string. Format ("{0:N3}",56789);//result:56,789.000STRINGSTR8 =string. Format ("{0:F1}",56789);//result:56789.0STRINGSTR9 =string. Format ("{0:F2}",56789);//result:56789.00Stringstr11 = (56789/100.0). ToString ("#.##");//result:567.89Stringstr12 = (56789/ -). ToString ("#.##");//result:567C or C currency Console.Write ("{0:C}",2.5);//$2.50Console.Write ("{0:C}", -2.5);//($2.50)D or D decimal number Console.Write ("{0:D5}", -);//00025e or E scientific type Console.Write ("{0:e}",250000);//2.500000E+005f or F fixed point Console.Write ("{0:F2}", -);//25.00Console.Write ("{0:f0}", -);// -G or G regular Console.Write ("{0:g}",2.5);//2.5N or N digital console.write ("{0:n}",2500000);//2,500,000.00x or x hex Console.Write ("{0:x}", -); /******************************************************************************/ /*how to ask Hovertree.com*/ASP. NET Set data format with String.Format Usage Summary (citation) {0:D} yy-mm-DD {0:P} percent 00.xx% {0: N2}12.68 {0: N0} - {0: C2} $12.68 {0:D}3/ at/2003 {0T A:xx:xxAM {0: male;; Female} DataGrid-The data formatting expression data formatting expression. NET Framework format expression, which is applied to the data before the data is displayed in the column. This expression consists of optional static text and a format specifier, represented in the following format: {0: Format specifier} 0 is a parameter index that indicates the data element to be formatted in the column, so 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. The formatting expression applied to this data type description Price: {0: C} numeric/decimaldisplays "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:D 4} integer (cannot be used with decimals.) Displays an integer in a field that is filled with four characters justifies by 0. {0: n2}%numeric Displays the digits that are exactly two digits after the decimal point, followed by the%". {0:000.0} numeric/decimalrounded 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 .,1996"). The date format depends on the culture settings of the page or Web. config file. {0:d} Date/datetime Short Date format (" A/ to/ About"). {0: yy-mm-DD} Date/datetime is the date represented by the year-month-day of the number ( the- ,- .). Read-only when this column is in edit mode, the data in the column is displayed in an editable control. 2006- Geneva- A| format--of ASP. NET Data formatdataformatstring We are presenting data, do not present the unmodified data to the consumer. For example the amount of $10,000 if we directly show "10000"may result in users being treated as 1000 or 100,000, which can cause problems with the user reading the data. If we finish the 10,000-dollar retouch and output it as "nt$Ten, thenot only makes it easier to read, it also allows users to reduce the chance of making mistakes. The following screen is the result of retouching: the 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 in dataformatstring the {0} represents the data itself, and the format string after the colon represents the format that you want the data to appear in, 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", 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$ A,345.68 "{0:C}"-12345.6789($ A,345.68) "{0:D}" 12345 12345 "{0:d8}" 12345 00012345 "{0:e}" 12345.67891234568e+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 A,345.68 "{0:N4}" 123456789 123,456,789.0000 "total: {0:c}" 12345.6789Total: $12345.68The commonly used date format is shown in the following table: Format description output format d Compact date format MM/dd/yyyy D Detailed date format dddd, MMMM DD, yyyy f full Format (LongDate + Shorttime) dddd, MMMM dd, yyyy hh:mm F full datetime format (LongDate +LongTime ) dddd, MMMM dd, yyyy HH:mm:ss G General Format ( ShortDate + ShortTime) mm/dd/yyyy hh:mm G General format ( ShortDate +LongTime) mm/dd/yyyy HH:mm:ss m,m Month Day format MMMM DD s moderate datetime format yyyy-mm-DD HH:MM:SS t compact time format hh:mm t verbose time format HH:MM:SSstringThe . 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:. ........- A,345.00%(R) Round-trip: ...-123.45(X) hexadecimal: ..... FFFFFF85 (d) Short Date: ... .6/ -/2004(D) Long Date: ..... Saturday, June -,2004(t) short time: ... ..8: OnePM (T) Long time: ...8: One:GenevaPM (f) Full date/ ShortTime:. . Saturday, June -,2004 8: OnePM (F) Full date/LongTime: ... Saturday, June -,2004 8: One:GenevaPM (g) General Date/ ShortTime:.6/ -/2004 8: OnePM (G) General Date/LongTime:.6/ -/2004 8: One:GenevaPM (M) Month: ..... June -(R) RFC1123: ..... Sat, -June2004 -: One:GenevaGMT (s) sortable: ........2004- .-26T20: One:Geneva(U) Universal sortable: ...2004- .- - -: One: 04Z (invariant) (U) Universal sortable: ... Sunday, June -,2004 3: One:GenevaAM (Y) Year: ..... June,2004(G) General: ..... Green (F) Flags: ..... Green (Flags or integer) (D) Decimal number: .....3(X) hexadecimal: .....00000003Description: String.Format replaces each format item in the specified String with the text equivalent of the value of the corresponding object. Example:intIvisit = -; stringSzName ="jackfled"; Response.Write (String.Format ("your account number is: {0}. {1} times were accessed.", SzName, ivisit));
C # various string formats