ToString data type format encyclopedia in C of thousandth

  • 2021-12-09 09:42:18
  • OfStack

Formatting GridView with DataFormatString
Display data in GridView, and the data to be displayed has many decimal places, so you want it to display only two decimal places. In delphi, just use DisplayFormat directly. In. net, check msdn for half a day, and find that this function can be realized by using DataFormatString, but how to set it doesn't work. Finally, it is found that due to security considerations of 2.0, HtmlEncode = false should be set at the same time, so that DataFormatString can take effect.
Leave a mark, and when you use it next time, you don't have to waste more time on N.
Also, DataFormatString = "{0: F}", which is the default format, displays two decimal places, and DataFormatString = "{0: Fn}" if the decimal places to be displayed are other values.

DataFormatString= "{0: format string}"

The {0} in DataFormatString represents the data itself, and the format string after the colon represents the format in which you want the data to appear;

Number, currency format:
After the specified format symbol, you can specify the number of digits to display 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 common numerical 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 and time formats:

Format description output format
d Reduced Date Format MM/dd/yyyy
D Detail 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 1 General Format (short date + short time) MM/dd/yyyy HH: mm
G 1 General Format (short date + long time) MM/dd/yyyy HH: mm: ss
m, M month-day format MMMM dd
s Moderate Date-Time Format yyyy-MM-dd HH: mm: ss
t Reduced 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

Routine

2.5.ToString("G")

2.5

N

Figures

2500000.ToString("N")

2,500,000.00

X

106-ary

255.ToString("X")

FF

formatCode is an optional formatting code string. (Please search "Format String" for details)
You must use '{' and '}' to separate the format from other characters. If you happen to use curly braces in the format, you can use two consecutive curly braces to represent one curly brace, that is, "{{" or "}}".

Examples of common formats:

(1) int i=12345;

this.textBox1.Text=i.ToString();

//Result 12345 (this refers to the current object, or 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 123

this.textBox2.Text=s2;

//Results the value is 123.450

(3) double i=12345.6789;

this. textBox1.Text=i. ToString ("f2"); //Result 12345.68

this.textBox2.Text=i.ToString("f6");

//Result 12345.678900

(4) double i=12345.6789;

this. textBox1.Text=i. ToString ("n"); //Outcome 12,345.68

this. textBox2.Text=i. ToString ("n4"); //Outcome 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");

//Outcome 03.5. 25

this. 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 indicates left alignment, accounting for 7 bits

this.textBox1.Text=s ;

//Results i: 123, j: 123.45


Related articles: