String.Format usage in C #

Source: Internet
Author: User

Tring. 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

1234567 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 / 100).ToString("#.##");             //result: 567

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

1 string.Format("{0:C}",0.2)

  

The result is: ¥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

1 string.Format("{0:C1}",23.15)

  

The result is: ¥23.2 (intercept will be automatically rounded)
Formatting multiple instances of object

1 string.Format("市场价:{0:C},优惠价{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)

12 string.Format("{0:D3}",23) //结果为:023string.Format("{0:D2}",1223) //结果为:1223,(精度说明符指示结果字符串中所需的最少数字个数。)

  

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

12 string.Format("{0:N}", 14200) //结果为:14,200.00 (默认为小数点后面两位)string.Format("{0:N3}", 14200.2458) //结果为:14,200.246 (自动四舍五入)

5. Percentage of formatting

12 string.Format("{0:P}", 0.24583) //结果为:24.58% (默认保留百分的两位小数)string.Format("{0:P1}", 0.24583) //结果为:24.6% (自动四舍五入)

6, zero placeholder and digit placeholder

1234 string , 12394.039)   //result: 12394.04 string , 194.039)   //result: 0194.04 string , 12394.039)   //result: 12394.04 string , 194.039)   //result: 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

123456789 string.Format("{0:d}",System.DateTime.Now) //结果为:2009-3-20 (月份位置不是03)string.Format("{0:D}",System.DateTime.Now) //结果为:2009年3月20日string.Format("{0:f}",System.DateTime.Now) //结果为:2009年3月20日 15:37 string.Format("{0:F}",System.DateTime.Now) //结果为:2009年3月20日 15:37:52 string.Format("{0:g}",System.DateTime.Now) //结果为:2009-3-20 15:38 string.Format("{0:G}",System.DateTime.Now) //结果为:2009-3-20 15:39:27 string.Format("{0:m}",System.DateTime.Now) //结果为:3月20日string.Format("{0:t}",System.DateTime.Now) //结果为:15:41 string.Format("{0:T}",System.DateTime.Now) //结果为:15:41:50

  

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.