String. Format (C #, VB. NET)

Source: Internet
Author: User
Tags rounds

String. Format (C #, VB. NET)

I used:

String. Format ("string: {0: D5}", 12)
"String: 00012" 'occupies 5 places. If not, use 0 instead.
String. Format ("string: {0, 5}", 12)
"String: 12" 'occupies 5 places. If not, use spaces instead. The right alignment
String. Format ("string: {0,-5}", 12)
"String: 12" 'occupies 5 places. If not, use spaces instead and align left.

? String. Format ("string: {0 :#, 0.0000}", 12)
"String: 12.0000"
? String. Format ("string: {0: #0, 0.00}", 12)
"String: 12.00"
? String. Format ("string: {0: #0, 0.00}", 12000)
"String: 12,000.00"
? String. Format ("string: {0: D #0.0000}", 12)
"String: d12.0000"
? String. Format ("string: {0: d2.5}", 12)
"String: d2125"
? String. Format ("string: {0: d0.0}", 12)
"String: d12.0"
? String. Format ("string: {0: d0.0000}", 12)
"String: d12.0000"
? String. Format ("string: {0: #00, 0.00}", 12000)
"String: 12,000.00"
? String. Format ("string: {0: #00, 0.00}", 12)
"String: 012.00"
? String. Format ("string: {0: #00, 0.00}", 1200)
"String: 1,200.00"
? String. Format ("string: {0: #000.00}", 1200)
"String: 1200.00"
? String. Format ("string: {0: #000.00}", 12)
"String: 012.00"
? String. Format ("string: {0: #000.00}", 1)
"String: 001.00"
Description of format characters
0
Zero placeholder
If the formatted value has a number at the position of "0" in the format string, the number is copied to the output 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 output string. The "00" specifier rounds the value to the number nearest to the decimal point, and the zero digit is rounded off. For example, if "00" is used to format 34.5, the value 35 is obtained.

#
Digit placeholder
If the formatted value contains a number at the position of "#" in the format string, the number is copied to the output string. Otherwise, no value is stored in the output 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. For example, format 34.5 with "#" to get the value 35.

.
Decimal point
The first "." character in the format string determines the location of the decimal point Separator in the formatted value; any other "." characters are ignored. The actual Characters Used as the decimal separator are determined by the numberdecimalseparator attribute of the numberformatinfo.

,
Thousands of separators and digital proportions
The "," character has two purposes. First, if the format string contains the "," character between the two digit placeholders (0 or #) on the left of the decimal point (if any, the output will insert a thousands separator between every three digits on the left of the decimal separator. The actual characters used as decimal separator in the output string are determined by the numbergroupseparator attribute of the currently formatted numberformatinfo.

Second, if the format string contains one or more "," characters on the left side of the decimal point, the number is divided by "," and multiplied by 1000 before formatting. For example, the format string "0," represents 100,000,000 as 100. Use the "," character to indicate that the proportion conversion does not include the thousands Separator in the formatted number. Therefore, to reduce the number by 1,000,000 times and insert a thousands separator, use the format string "#, #0 ,,".
 
%
Percentage placeholder
If the "%" character appears in the format string, the number is multiplied by 100 before formatting. Insert the appropriate symbol to the position where the number itself appears "%" in the format string. The percentage characters used are determined by the current numberformatinfo class.
 

Commonly used: Convert string type to string type
12345. tostring ("N"); // generate 12,345.00
12345. tostring ("C"); // generate $12,345.00
12345. tostring ("e"); // generate 1.234500e + 004
12345. tostring ("F4"); // generate 12345.0000
12345. tostring ("X"); // generate 3039 (hexadecimal)
12345. tostring ("p"); // generate 1,234,500.00%

C # formatting the numeric result table (formatting strings)

C currency string. Format ("{0: C3}", 2) $2.000
D decimal string. Format ("{0: D3}", 2) 002
E scientific notation 1.20e + 001 1.20e + 001
G regular string. Format ("{0: g}", 2) 2
N string. Format ("{0: n}", 250000) 250,000.00 separated by semicolons
X hexadecimal string. Format ("{0: x000}", 12) C

String. Format ("{:000. 000}", 12.2) 012.200

Msdn formatting Overview:

Http://msdn.microsoft.com/zh-cn/library/26etazsy.aspx

Msdn compound formatting:

Http://msdn.microsoft.com/zh-cn/library/txafckwd.aspx

Custom numeric format string

Http://msdn.microsoft.com/zh-cn/library/0c899ak8.aspx

Additional content:

Source: http://hi.baidu.com/wlx_sm/blog/item/d04b898b24bfe3d2fc1f1030.html


Stringstr1 = string. Format ("{0: N1}", 56789); // results: 56,789.0
Stringstr2 = string. Format ("{0: N2}", 56789); // results: 56,789.00
Stringstr3 = string. Format ("{0: N3}", 56789); // results: 56,789.000
Stringstr8 = string. Format ("{0: F1}", 56789); // results: 56789.0
Stringstr9 = string. Format ("{0: F2}", 56789); // results: 56789.00
Stringstr11 = (56789/100 .0). tostring ("#. #"); // result: 567.89
Stringstr12 = (56789/100). tostring ("#. #"); // result: 567

C or C
Currency
Console. Write ("{0: c}", 2.5); // $2.50
Console. Write ("{0: c}",-2.5); // ($2.50)

D Or d
Decimal number
Console. Write ("{0: D5}", 25); // 00025

E or E
Scientific type
Console. Write ("{0: e}", 250000); // 2.500000e + 005

F or F
Fixed Point
Console. Write ("{0: F2}", 25); // 25.00
Console. Write ("{0: F0}", 25); // 25

G or G
General
Console. Write ("{0: g}", 2.5); // 2.5

N or N
Number
Console. Write ("{0: n}", 2500000); // 2,500,000.00

X or X
Hexadecimal
Console. Write ("{0: x}", 250); // fa
Console. Write ("{0: x}", 0 xFFFF); // FFFF
 

------------------------

More details:

Www.yaosansi.com/post/360.html

Http://blog.csdn.net/sunchaohuang/archive/2009/02/16/3894808.aspx

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/linjimu/archive/2009/04/18/4090575.aspx

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.