C # digit formatting

Source: Internet
Author: User

Complete format: {index [, width] [: formatstring]}

Index is in this formatProgramParameters after the referenced format string are counted from scratch. Width (optional) indicates the width of the field to be formatted. positive value indicates the right alignment of the result, and negative value indicates the left alignment of the result; formatstring (optional), which contains the format description for setting the Type format.

1. built-in letter formats

1. E-scientific notation Representation

25000. tostring ("e"); // result: 2.500000e + 004

2. C-currency representation, with a comma separator. By default, two digits are retained after the decimal point.

2.5.tostring ("C"); // result:¥2.50

3. D-decimal number

25. tostring ("D5"); // result: 00025

4. F-floating point number, reserved decimal places (rounded)

25. tostring ("F2"); // result: 25.00

5. G-regular, retain valid digits of the specified number of digits, rounding

2.52.tostring ("G2"); // result: 2.5

6. N-separated by commas (,). By default, two digits are retained after the decimal point.
2500000. tostring ("N"); // result: 2,500,000.00

7. X-hexadecimal, non-integer type will generate format exception

255. tostring ("X"); // result: FF

Ii. Custom mode output

1. "0" Description: placeholder. If possible, fill the bit

String. Format ("{0: 00}", 1234); // result: 001234

2. "#" Description: A placeholder. If possible, fill the position.

String. Format ("{0 :######}", 1234); // result: 1234

String. Format ("{0: #0 ####}", 1234); // result: 01234

String. Format ("{0: 0 #0 ####}", 1234); // result: 0001234

3. "." Description: decimal point

String. Format ("{0: 000. 000}", 1234); // result: 1234.000

String. Format ("{0: 000. 000}", 4321.12543); // result: 4321.125

4. "," Description: Numeric grouping, also used for the Multiplier

String. Format ("{0, 0}", 1234567); // result: 1,234,567

5. "%" Description: The format is percent.

String. Format ("{0: 0%}", 1234); // result: 123400%

String. Format ("{0: # %}", 1234.125); // result: 123413%

String. Format ("{0. 00%}", 1234); // result: 123400.00%

String. Format ("{0: #. 00%}", 1234.125); // result: 123412.50%

6. "ABC" Description: displays text in single quotes

String. Format ("{0: 'text' 0}", 1234); // result: Text 1234

7. "\" Description: used for transfer operator

String. Format ("\" ABC! \ ""); // Result: ABC!

8. "@" Description: followed by the string to be printed

String. Format (@ "" ABC! ""); // Result: ABC!

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.