C # string. Format Usage Description

Source: Internet
Author: User

string. Five types of overloads for format:

String. Format (String,object) replaces one or more formatting items in the specified string with the string representation of the specified object.
String. The format (string,object,object) in the specified string is replaced with the string representation of two specified objects.
String. The format (string,object,object) in the specified string is replaced with the string representation of three specified objects.
String. Format (string,params object[]) replaces the formatting item in the specified string with the string representation of the corresponding object in the specified array.
String. Format (system.iformatprovider,string,params object[]) replaces the formatting item in the specified string with the string representation of the corresponding object in the specified array. The specified parameters provide culture-specific formatting information.

commonly used numeric formatting commands:

character Description Sample Output
C currency string. Format ("{0:c}", 9) ¥9.00
D decimal string. Format ("{0:d}", 9) 9
E Scientific Count method string. Format ("{0:e}", 12345) 1.234500E+004
F decimal string. Format ("{0:f}", 12345) 12345.00
G general string. Format ("{0:g}", 12345) 12345
\ N A number separated by commas string. Format ("{0:n}", 12345) 12,345.00
P percent string. Format ("{0:p}", 0.12345) 12.35%
X hex string. Format ("{0:x}", a) C
placeholder string. Format ("{0:000.000}", 12.3) 012.300
1. Format to monetary form

String.   Format ("{0:c}", 9); ¥9.00 (English operating system results: $9.00)
String. Format ("{0:c1}", 9.67); ¥9.7 (Automatic rounding when intercepting)
String.    Format ("{0:c}", 9); ¥9.00
String.  Format ("{0:c1}", 9.67); ¥9.7

Format multiple Object objects
String. Format ("Original price {0:C1}, activity price {1:C3}", 9,7.8666); The original price is ¥9.0, the activity price ¥7.867 2, the format is decimal form (integral type only)

String.    Format ("{0:d}", 9); 9
String.    Format ("{0:d3}", 9); 009, the precision represents the minimum length of the result string
String. Format ("{0:d3}", 12345); 12345, the result string is not less than the length of the data original degree
String.    Format ("{0:d}", 9); 9
String.    Format ("{0:d3}", 9); 009
String. Format ("{0:d3}", 12345); 12345 3, format into scientific notation form

String. Format ("{0:e}", 12345); 1.234500E+004
String. Format ("{0:e3}", 12345); 1.235E+004
String. Format ("{0:e}", 12345); 1.234500e+004
String. Format ("{0:e3}", 12345); 1.235E+004 4, formatted as a decimal form

String. Format ("{0:f}", 123.678); 123.68, the default retention of two decimal digits and automatically rounding when intercepted
String. Format ("{0:f3}", 123.678); 123.678
String. Format ("{0:f}", 123.678); 123.68
String. Format ("{0:f3}", 123.678); 123.678 5, formatted as a regular form

String. Format ("{0:g}", 12345); 12345
String. Format ("{0:g3}", 12345); 1.23E+04
String. Format ("{0:g}", 12345); 12345
String. Format ("{0:g3}", 12345); 1.23E+04 6, formatted as a comma-separated number form

String. Format ("{N}", 12345); 12,345.00, leave two decimal digits by default
String. Format ("{N3}", 12345); 12,345.000
String. Format ("{n}", 12345); 12,345.00
String. Format ("{n3}", 12345); 12,345.000 7, formatted as a percentage form

String. Format ("{0:p}", 0.12345); 12.35%, the default retention of two decimal digits and automatically rounding when intercepted
String. Format ("{0:p3}", 0.12345); 12.345%
String. Format ("{0:p}", 0.12345); 12.35%
String. Format ("{0:p3}", 0.12345); 12.345% 8, formatted as 16 (integer only)

String. Format ("{0:x}", 12); C
String. Format ("{0:x3}", 12); 00C
String. Format ("{0:x}", 12); C
String. Format ("{0:x3}", 12); 00C 9, formatted as placeholder form

String. Format ("{0:000.00}", 12.056); 012.06
String. Format ("{0:000.00}", 12.003); 012.00
String.   Format ("{0:000.00}", 12.3); 012.30
String. Format ("{0:###.##}", 12.056); 12.06
String. Format ("{0:###.##}", 12.003); 12
String.  Format ("{0:###.##}", 12.3); 12.3

Zero placeholder: If the formatted value has a number in the position of "0" in the format string, the number is copied into the result string. The position of the leftmost "0" before the decimal point and the position of the rightmost "0" after the decimal point determine the range of digits that always appear in the result string. The "00" format string causes the value to be rounded to a digit before the decimal point, where 0 is not left out.
Digit placeholder: If the formatted value has a number in the position of "#" in the format string, the number is copied into the result string. Otherwise, no value is stored at this location in the result string. The "# #" format string causes the value to be rounded to a digit before the decimal point, of which 0 is always to be shed.
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.

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.