Summary of ToString data type format Daquan (thousand characters) in C #

Source: Internet
Author: User
This article mainly introduces C # in the ToString data type format encyclopedia, the need for friends can refer to the following

Formatting the GridView with DataFormatString
In the GridView display data, to display the data has a lot of decimal places, just want it to show only two decimal places, in Delphi, directly with DisplayFormat, in. NET, checked a half-day MSDN, found that using dataformatstring can achieve this function, but how to set it does not work, and finally found that, because of 2.0 for security reasons, but also set HTMLEncode = False, to enable dataformatstring to take effect.
Leave a mark, the next time you use, you don't have to waste n more time.
Also, dataformatstring = "{0:f}", which is the default format, displays two decimal places, if the number of decimal places to display is a different value, dataformatstring = "{0:FN}".

Dataformatstring= "{0: format string}"

The {0} in dataformatstring represents the data itself, and the format string after the colon represents the format that the data is expected to display;

Number, Currency format:
After the specified format symbol, you can specify the number of digits to display for the decimal. For example, the original data is "1.56", and if the format is set to {0:n1}, the output is "1.5". Its commonly used numeric format is shown in the following table:

format string input result

"{0:c}" 12345.6789 $12,345.68 "{0:c}" -12345.6789 ($12,345.68) "{0:d}" 12345 12345 "{0:d8}" 12345 00012345 "{0:e}" 12345.6789 1234568E+004 "{0:e10}" 12345.6789 1.2345678900E+004 "{0:f}" 12345.6789 12345.68 "{0:f0}" 12345.6789 12346 "{0: G} "12345.6789 12345.6789" {0:g7} "123456789 1.234568E8" {0:n} "12345.6789 12,345.68" {0:n4} "123456789 123,456,789.0000" Total: {0:c} ' 12345.6789 total: $12345.68

Common Date-time formats:

Format Description Output format
D Thin Date format mm/dd/yyyy
D Detailed date format dddd, MMMM DD, yyyy
F Full Format (long date + short time) dddd, MMMM dd, yyyy hh:mm
F
Full Date Time format
(Long date + long time)
dddd, MMMM dd, yyyy HH:mm:ss
G General format (short date + short time) mm/dd/yyyy hh:mm
G General Format (short date + long time) mm/dd/yyyy HH:mm:ss
M,m month-day format MMMM DD
s medium Date time format Yyyy-mm-dd HH:MM:SS
T Lite time Format hh:mm
T Verbose time Format HH:mm:ss

C

Currency

2.5.ToString ("C")

¥2.50

D

Decimal number

25.ToString ("D5")

00025

E

Scientific type

25000.ToString ("E")

2.500000E+005

F

Fixed point

25.ToString ("F2")

25.00

G

Conventional

2.5.ToString ("G")

2.5

N

Digital

2500000.ToString ("N")

2,500,000.00

X

Hexadecimal

255.ToString ("X")

Ff

Formatcode is an optional format code string. (For more information, please search for "formatted string" view)
You must use "{" and "}" to separate the format from other characters. If you want to use curly braces exactly in the format, you can use two consecutive curly braces to denote a curly brace, which is: "{{" or "}}".

Examples of common formats:

(1) int i=12345;

This.textbox1.text=i.tostring ();//Result 12345 (this refers to the current object, or an instance of the current class) this.textbox2.text=i.tostring ("D8");//Result 00012345

(2) int i=123;

Double j=123.45;string s1=string. Format ("The value is {0,7:d}", i); string s2=string. Format ("The value is {0,7:f3}", j); this.textbox1.text=s1;//Results The value is 123this.textbox2.text=s2;//the result of the value is 123. 450

(3) Double i=12345.6789;

This.textbox1.text=i.tostring ("F2"); Result 12345.68this.textbox2.text=i.tostring ("F6");//Result 12345.678900

(4) Double i=12345.6789;

This.textbox1.text=i.tostring ("n"); Results 12,345.68this.textbox2.text=i.tostring ("N4"); Result 12,345.6789

(5) Double i=0.126;

String s=string. Format ("The value is {0:p}", i); This.textbox1.text=i.tostring ("P"); Results 12.6%this.textbox2.text=s; Results The value is 12.6%

(6) DateTime dt =new datetime (2003,5,25);

This.textbox1.text=dt. ToString ("yy. M.D ");//Results 03.5.25this.textbox2.text=dt. ToString ("YYYY year m Month");//Results May 2003 Convert.todatetime ("2005/12/22 22:22:22"). ToString ("Yyyy/mm/dd HH:mm:ss") "2005/12/22 22:22:22"

(7) int i=123;

Double j=123.45;string s=string. Format ("i:{0,-7},j:{1,7}", i,j),//-7 = left-aligned, 7-bit this.textbox1.text=s;//result i:123, j:123.45
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.