Detailed description of string. Format formatting usage

Source: Internet
Author: User
Tags rounds

1. format the currency (related to the system environment. The Chinese system uses the default format of RMB and the English system uses the format of USD)

String. Format ("{0: c}", 0.2): ¥0.20 (English OS result: $0.20)

By default, two decimal places are retained after the decimal point. to retain one or more digits, you can specify the number of digits.
String. Format ("{0: C1}", 23.15): ¥23.2 (the screenshot is automatically rounded up)

Format Multiple object instances
String. Format ("Market Price: {0: c}, discount price {1: c}", 23.15, 19.82)

2. Format a decimal number (formatted as a fixed number of digits. The number of digits cannot be less than the number before formatting. Only integer data is supported)

String. Format ("{0: D3}", 23): 023

String. Format ("{0: D2}", 1223) returns 1223, indicating the minimum number of digits required in the result string .)

3. Separate the numbers with semicolons and specify the digits after the decimal point

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

String. Format ("{0: N3}", 14200.2458): 14,200.246 (Auto rounding)

4. Formatting percentage

String. Format ("{0: p}", 0.24583): 24.58% (two decimal places in the percentage are reserved by default)

String. Format ("{0: P1}", 0.24583): 24.6% (Auto rounding)

5. Zero placeholders andDigit placeholder

String. Format ("{0: 0000. 00}", 12394.039): 12394.04

String. Format ("{0: 0000. 00}", 194.039): 0194.04

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

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

The following section is hard to understand. You can test the actual application to understand it.
Zero placeholder:
If the formatted value has a number at the position of "0" in the format string, the number is copied to the result string. The leftmost "0" position before the decimal point and the rightmost "0" position after the decimal point determine the total number range in the result string.
The "00" specifier rounds the value to the number nearest to the decimal point, and the zero digit is rounded off.

Digit placeholder:
If the formatted value contains a number at the position of "#" in the format string, the number is copied to the result string. Otherwise, no value is stored in this position in the result string.
Note that if "0" is not a valid number, this specifier never displays "0", even if "0" is a unique number in the string. If "0" is a valid number in the displayed number, "0" is displayed.
A string in the format of "#" rounds the value to the number nearest to the decimal point, with zero rounded off.

PS: Space placeholder

String. Format ("{0,-50}", theobj); // format it to 50 characters. The original characters are left aligned. If not, fill in spaces.
String. Format ("{}", theobj); // format it to 50 characters. The original character is right-aligned. If not, fill in spaces.

6. Date formatting

String. Format ("{0: d}", system. datetime. Now): 2009-3-20 (the month is not 03)

String. Format ("{0: d}", system. datetime. Now): January 1, March 20, 2009

String. Format ("{0: f}", system. datetime. Now): March 20, 2009

String. Format ("{0: f}", system. datetime. Now): March 20, 2009 15:37:52

String. Format ("{0: g}", system. datetime. Now ):

String. Format ("{0: g}", system. datetime. Now): 15:39:27

String. Format ("{0: m}", system. datetime. Now): January 1, March 20

String. Format ("{0: t}", system. datetime. Now): 15: 41

String. Format ("{0: t}", system. datetime. Now): 15: 41: 50

Detailed description of string. Format formatting usage

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.