C common data format conversion summary

  • 2020-05-19 05:41:10
  • OfStack

DataFormatString format GridView to display data in GridView, you want to display data with many decimal places, you want it to display only two decimal places, in delphi, you just use DisplayFormat, In. net checked along while msdn, found that using DataFormatString can achieve this function, how set doesn't work, but finally found that due to 2.0 due to security considerations, and at the same time set up HtmlEncode = false, can make effective DataFormatString. Leave a mark, the next time, and use it every time you wouldn't have wasted N much time. And also, DataFormatString = "{0: F}", is the default format, according to two decimal places, if need to display the number of decimal digits for other values, DataFormatString = "{0: Fn}".

DataFormatString="{0: format string}"

The {0} in DataFormatString represents the data itself, while the format string after the colon represents the format in which the data is expected to be displayed.

Numeric, currency format: you can specify the number of digits that a decimal will display after the specified format symbol. For example, the original data is "1.56", if the format is set to {0:N1}, then the output is "1.5". The commonly used 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}" {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:

d output 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 and time format (long date + long time) dddd, MMMM dd, yyyy HH: ss g 1 (short date + short time) MM/dd/yyyy HH mm MM/dd yyyy HH:mm ss m,M MMMM dd s moderate date and time yyyy-MM-dd HH:mm:ss t HH: mm mm:ss

C

currency

2.5.ToString("C")

RMB 2.50

D

Decimal number

25.ToString("D5")

00025

E

science-based

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

106 into the system

255.ToString("X")

FF

formatCode is an optional formatting code string. (search "formatted string" for details)

You must use '{' and'} 'to separate the format from the other characters. If you happen to be using curly braces in the format, you can use two consecutive curly braces to represent one curly brace, i.e., "{{" 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");

00012345 / / results

(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 ;

// the result is 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 "); / / 12345.68 as a result

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

/ / 12345.678900 as a result

(4) double i=12345.6789;

this. textBox1. Text = i. ToString (" n "); / / results 12345.68

this. textBox2. Text = i. ToString (" n4 "); 12345678 / / results

(5) double i=0.126;

string s=string.Format("the value is {0:p}",i);

this. textBox1. Text = i. ToString (" p "); / / the result by 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");

/ / 03.5.25 results

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 means left-aligned, accounting for 7 bits

this.textBox1.Text=s ;

// results i:123,j: 123.45


Related articles: