Format and usage of String.Format in C #

Source: Internet
Author: User

String. Several definitions of the Format method:

String.Format (String, Object) replaces the format item in the specified String with the text equivalent of the value of the specified Object instance.


String.Format (String, object[]) replaces the format item in the specified string with the text equivalent of the value of the corresponding Object instance in the specified array.


String.Format (IFormatProvider, String, object[]) replaces the format item in the specified string with the text equivalent of the value of the corresponding Object instance in the specified array. The specified parameter provides culture-specific formatting information.


String.Format (String, Object, object) replaces the format item in the specified string with the text equivalent of a value of two specified Object instance.

String.Format (String, Object, object, object) replaces the format item in the specified string with the text equivalent of a value of three specified Object instance.

Commonly used formatted numeric results table

Character

Description

Example

Output

C Currency string. Format("{0:c3}", 2) $2.000
D Decimal string. Format("{0:d3}", 2) 002
E Scientific counting method 1.20E+001 1.20E+001
G Conventional string. Format("{0:g}", 2) 2
N Numbers separated by semicolons string. Format("{0:n}", 250000) 250,000.00
X Hexadecimal string. Format("{0:x000}", 12) C
string. Format("{0:000.000}", 12.2) 012.200

Several examples of common use

1. Number format for strings
String str1 =string. Format ("{0:n1}", 56789);               result:56,789.0
String str2 =string. Format ("{0:n2}", 56789);               result:56,789.00
String Str3 =string. Format ("{0:n3}", 56789);               result:56,789.000
String Str8 =string. Format ("{0:f1}", 56789);               result:56789.0
String Str9 =string. Format ("{0:f2}", 56789);               result:56789.00
String str11 = (56789/100.0). ToString ("#.##");           result:567.89
String str12 = (56789/100). ToString ("#.##");             result:567

2. Formatted currency (related to system environment, Chinese system default format RMB, English system format USD)

String. Format ("{0:c}", 0.2) results are: ¥0.20 (English operating system results: $0.20)

The default formatting retains two decimal places after the decimal point, and if you need to keep one or more, you can specify the number of digits
String. Format ("{0:c1}", 23.15) Result: ¥23.2 (intercept will be automatically rounded)

Formatting multiple instances of object
String. Format ("market price: {0:C}, preferential price {1:C}", 23.15,19.82)

3, formatted decimal number (formatted as a fixed number of digits, the number of digits can not be less than unformatted before, only support shaping)

String. Format ("{0:d3}", 23) result is: 023

String. The result of Format ("{0:d2}", 1223) is: 1223, (the precision specifier indicates the minimum number of digits required in the resulting string.) )

4. A semicolon-delimited number, and specify the number of digits after the decimal point

String. Format ("{0:n}", 14200) Results: 14,200.00 (default is two digits after decimal point)

String. Format ("{0:n3}", 14200.2458) Result: 14,200.246 (auto rounding)

5. Percentage of formatting

String. Format ("{0:p}", 0.24583) result is: 24.58% (default of two decimal places reserved)

String. Format ("{0:p1}", 0.24583) result is: 24.6% (auto rounding)

6, zero placeholder and digit placeholder

String. The result of Format ("{0:0000.00}", 12394.039) is: 12394.04

String. The result of Format ("{0:0000.00}", 194.039) is: 0194.04

String. Format ("{0:###.##}", 12394.039) result is: 12394.04

String. Format ("{0:####.#}", 194.039) result is: 194

The following explanation is difficult to understand, more test the actual application can be understood.


Zero placeholder:
If the formatted value has a number in the format string where "0" appears, this number is copied to the result string. The position of the leftmost "0" before the decimal point and the rightmost "0" after the decimal point determine the total number range that appears in the result string.
The "00" specifier causes the value to be rounded to the nearest digit before the decimal point, where 0 bits are always removed.

Digit placeholder:
If the formatted value has a number in the format string where "#" appears, the number is copied to the result string. Otherwise, no value is stored at this location in the resulting string.


Note that if "0" is not a valid number, this specifier never displays the "0" character, even if "0" is the only number in the string. If "0" is a valid number in the displayed number, the "0" character is displayed.
The "# #" format string allows the value to be rounded to the nearest digit before the decimal point, where 0 is always removed.

7. Date formatting

String. Format ("{0:d}", System.DateTime.Now) result is: 2009-3-20 (month position is not 03)
String. Format ("{0:d}", System.DateTime.Now) results are: March 20, 2009
String. Format ("{0:f}", System.DateTime.Now) results are: March 20, 2009 15:37
String. Format ("{0:f}", System.DateTime.Now) results are: March 20, 2009 15:37:52
String. Format ("{0:g}", System.DateTime.Now) results are: 2009-3-20 15:38
String. Format ("{0:g}", System.DateTime.Now) results are: 2009-3-20 15:39:27
String. Format ("{0:m}", System.DateTime.Now) results are: March 20
String. The result of Format ("{0:t}", System.DateTime.Now) is: 15:41
String. The result of Format ("{0:t}", System.DateTime.Now) is: 15:41:50

Format and usage of String.Format in C #

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.