C string.format

  • 2020-11-25 07:29:53
  • OfStack

This example summarizes the string. format usage in C#. Share to everybody for everybody reference. The specific analysis is as follows:

Several definitions of the ES6en. 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 parameters provide culture-specific formatting information.
String. Format (String, Object, Object) replaces the format item in the specified String with the text equivalent of the value of the two specified Object instances.
String.Format (String, Object, Object, Object) replaces the format item in the specified String with the text equivalent of the value of the three specified Object instances.

A commonly formatted numeric result table

字符

说明

示例

输出

C 货币 string.Format("{0:C3}", 2) $2.000
D 10进制 string.Format("{0:D3}", 2) 002
E 科学计数法 1.20E+001 1.20E+001
G 常规 string.Format("{0:G}", 2) 2
N 用分号隔开的数字 string.Format("{0:N}", 250000) 250,000.00
X 106进制 string.Format("{0:X000}", 12) C
    string.Format("{0:000.000}", 12.2) 012.200
Common examples

1. Numeric format of string

 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. Formatting currency (related to the environment of the system, Chinese system will format RMB by default, while English system will format US dollar)
string.Format("{0:C}",0.2)
Result: ¥0.20 (English operating system result: $0.20)
The default format keeps two decimal places behind the decimal point, or if you want to keep one or more digits, you can specify the number of digits
string.Format("{0:C1}",23.15)
The result is: ¥23.2 (interception will automatically round 4 into 5)
Format multiple Object instances
string.Format(" Market price: {0:C} The preferential price {1:C}",23.15,19.82)


3, formatting decimal Numbers (formatting to a fixed number of digits, not less than the number of digits before formatting, only supports shaping)
string.Format("{0:D3}",23) // The result is: 023
string.Format("{0:D2}",1223) // The result is: 1223 The precision specifier indicates the minimum number of Numbers required in the resulting string.


4. A number separated by a semicolon and specifying the number of digits after the decimal point
string.Format("{0:N}", 14200) // The result is: 14,200.00  (Default is two digits behind the decimal point) 
string.Format("{0:N3}", 14200.2458) // The result is: 14,200.246 (to be automatic 4 Give up 5 In)
 
5 , formatting percentage
string.Format("{0:P}", 0.24583) // The result is: 24.58% (The default is to keep two decimal places for hundredths)
string.Format("{0:P1}", 0.24583) // The result is: 24.6% (to be automatic 4 Give up 5 In)


6. Zero and number placeholders
string.Format("{0:0000.00}", 12394.039) // The result is: 12394.04
string.Format("{0:0000.00}", 194.039) // The result is: 0194.04
string.Format("{0:###.##}", 12394.039) // The result is: 12394.04
string.Format("{0:####.#}", 194.039) // The result is: 194


The following explanation is a bit more difficult to understand, but more testing 1 will show you how it works in practice.
Zero placeholder: If the formatted value has 1 number where the "0" appears in the format string, the number is copied to the result string. The left-most "0" position before the decimal point and the right-most "0" position after the decimal point determine the range of Numbers that will always appear in the result string. The "00" specifier causes the value to be rounded to the nearest number before the decimal point, where zero is always truncated.
Number placeholder: If the formatted value has 1 number in the "#" position in the format string, the number is copied to the result string. Otherwise, no value is stored at this location in the result string.
Note that if "0" is not a significant number, this specifier never displays the "0" character, even if "0" is the only one in the string. If "0" is a significant number in the displayed number, the "0" character is displayed. The "##" format string causes the value to be rounded to the nearest number before the decimal point, where zero is always dropped.

7. Date formatting

string.Format("{0:d}",System.DateTime.Now) // The result is: 2009-3-20  (The month position is not 03 ) 
string.Format("{0:D}",System.DateTime.Now) // The result is: 2009 years 3 month 20 day
string.Format("{0:f}",System.DateTime.Now) // The result is: 2009 years 3 month 20 day 15:37
string.Format("{0:F}",System.DateTime.Now) // The result is: 2009 years 3 month 20 day 15:37:52
string.Format("{0:g}",System.DateTime.Now) // The result is: 2009-3-20 15:38
string.Format("{0:G}",System.DateTime.Now) // The result is: 2009-3-20 15:39:27
string.Format("{0:m}",System.DateTime.Now) // The result is: 3 month 20 day
string.Format("{0:t}",System.DateTime.Now) // The result is: 15:41
string.Format("{0:T}",System.DateTime.Now) // The result is: 15:41:50

Hopefully this article has helped you with your C# programming.


Related articles: