. NET string format

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/jobs2/p/3948049.html

Transferred from: http://jingyan.baidu.com/article/48206aeaf8c52f216ad6b300.html

1, formatted currency (related to system environment, Chinese system default format RMB, English system format USD)string. Format ("{0:C}",0.2) result is: ¥0.20(English operating system results: $0.20The default format is to retain two decimal places after the decimal point, and if you need to keep one or more, you can specify the number of digitsstring. Format ("{0:C1}",23.15) result is: ¥23.2(intercepts are rounded automatically) format multiple object instancesstring. Format ("Market price: {0:C}, preferential price {1:C}",23.15,19.82)2, formatting decimal digits (formatted as a fixed number of digits, the number of digits cannot be less than unformatted, only support shaping)string. Format ("{0:D3}", at) The result is:023string. Format ("{0:D2}",1223) The result is:1223, (the precision specifier indicates the minimum number of digits required in the resulting string.) )3, separated by semicolons, and specifying the number of digits after the decimal pointstring. Format ("{0:n}",14200) The result is: -,200.00(default is two digits after the decimal point)string. Format ("{0:N3}",14200.2458) The result is: -,200.246(Auto rounding)4, formatted percentagestring. Format ("{0:p}",0.24583) The result is:24.58%(two decimal places are reserved by default)string. Format ("{0:P1}",0.24583) The result is:24.6%(Auto rounding)5, zero placeholders, and digit placeholdersstring. Format ("{0:0000.00}",12394.039) The result is:12394.04string. Format ("{0:0000.00}",194.039) The result is:0194.04string. Format ("{0:###.##}",12394.039) The result is:12394.04string. Format ("{0:####.#}",194.039) The result is:194The following explanation is difficult to understand, more test the actual application can be understood. Zero placeholder: If the formatted value appears in the format string "0is a number, the number is copied to the result string. The leftmost "before the decimal point.0"The location and the far right after the decimal point"0position determines the number range that always appears in the result string. "xxthe 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. Please note that if the "0"is not a valid number, this specifier never appears"0"Character, even if"0"Is the only number in the string. If0is a valid number in the displayed number, it displays the0"character. The "# #" format string allows the value to be rounded to the nearest digit before the decimal point, where 0 is always removed. PS: Space Placeholderstring. Format ("{0,-50}", theobj);//format to 50 characters, the original word aligns, insufficient to fill the spacestring. Format ("{0,50}", theobj);//format to 50 characters, the original word aligns, insufficient to fill the space6, date formattingstring. Format ("{0:D}", System.DateTime.Now) results are: the-3- -(The month location is not the same)string. Format ("{0:D}", System.DateTime.Now) The result is: March 20, 2009string. Format ("{0:f}", System.DateTime.Now) The result is: March 20, 2009 the:Panax Notoginsengstring. Format ("{0:f}", System.DateTime.Now) The result is: March 20, 2009 the:Panax Notoginseng: thestring. Format ("{0:g}", System.DateTime.Now) results are: the-3- -  the: -string. Format ("{0:g}", System.DateTime.Now) results are: the-3- -  the: the: -string. Format ("{0:m}", System.DateTime.Now) The result is: March 20string. Format ("{0:t}", System.DateTime.Now) results are: the: Astring. Format ("{0:t}", System.DateTime.Now) results are: the: A: -

To format a table of values:

Basic:

Digital:

    

      

Date:

      

. NET string format

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.