String.Format usage in C #

Source: Internet
Author: User

This article mainly introduces the use of String.Format in C #, in the case of the form of a more detailed description of the various uses of String.Format format, very practical value, the need for friends can refer to the following

This example summarizes String.Format usage in C #. Share to everyone for your reference. The specific analysis is as follows:

Several definitions of the String.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 commonly used examples 1, the number format of the string

The code is as follows:
1 stringSTR1 =string. Format ("{0:N1}",56789);//result:56,789.02  stringSTR2 =string. Format ("{0:N2}",56789);//result:56,789.003  stringSTR3 =string. Format ("{0:N3}",56789);//result:56,789.0004  stringStr8 =string. Format ("{0:F1}",56789);//result:56789.05  stringSTR9 =string. Format ("{0:F2}",56789);//result:56789.006  stringStr11 = (56789/100.0). ToString ("#.##");//result:567.897  stringStr12 = (56789/ -). ToString ("#.##");//result:567

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

The code is as follows:
1 string. Format ("{0:c}",0.2)

The result is: ¥0.20 (English operating system results: $0.20) The default formatting is two decimal places after the decimal point, and if you need to keep one or more, you can specify the number of digits

The code is as follows:
1 string. Format ("{0:c1}",23.15)

The result is: ¥23.2 (intercept automatically rounds) format multiple instance of object

The code is as follows:
1 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)

The code is as follows:
1 string. Format ("{0:d3}", with a result of: 0232string. Format ("{0:d2}",1223// result 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

The code is as follows:
1 string. Format ("{0:n}"14200// result is: 14,200.00 (default is two digits after decimal)2 string. Format ("{0:n3}"14200.2458// Result: 14,200.246 (auto rounding) 


5. Percentage of formatting

1 string. Format ("{0:p}"0.24583// result is: 24.58% (the default is two decimal places reserved)2 string. Format ("{0:p1}"0.24583// result is: 24.6% (auto rounding)

6, zero placeholder and digit placeholder

The code is as follows:
1 string. Format ("{0:0000.00}",12394.039)//The result is: 12394.042 string. Format ("{0:0000.00}",194.039)//The result is: 0194.043 string. Format ("{0:###.##}",12394.039)//The result is: 12394.044 string. Format ("{0:####.#}",194.039)//The result is: 194

The following explanation is difficult to understand, more test the actual application can be understood. Zero placeholder: If a 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, this 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

The code is as follows:
1 string. Format ("{0:D}", System.DateTime.Now)//The result is: 2009-3-20 (month position is not)2 string. Format ("{0:D}", System.DateTime.Now)//results are: March 20, 20093 string. Format ("{0:f}", System.DateTime.Now)//The result is: March 20, 2009 15:374 string. Format ("{0:f}", System.DateTime.Now)//The result is: March 20, 2009 15:37:525 string. Format ("{0:g}", System.DateTime.Now)//The result: 2009-3-20 15:386 string. Format ("{0:g}", System.DateTime.Now)//The result: 2009-3-20 15:39:277 string. Format ("{0:m}", System.DateTime.Now)//results are: March 208 string. Format ("{0:t}", System.DateTime.Now)//The result is: 15:419 string. Format ("{0:t}", System.DateTime.Now)//The result is: 15:41:50

I hope this article is helpful to everyone's C # programming.

Transferred from: http://www.jb51.net/article/57216.htm

String.Format usage in C #

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.