// format is sring output
label1.text = string.format ("asdfadsf {} adsfasdf", a);
Label2.Text = "asdfadsf"+a.ToString()+"adsfasdf";
Label1.Text = string.Format("asdfadsf{:C}adsfasdf",a); //asdfadsf¥1,234.adsfasdf
Label2.Text = "asdfadsf"+a.ToString("C")+"adsfasdf"; //asdfadsf¥1,234.adsfasdf
double b = 1234.12543;
int a = 12345678;
// output in a special string style
label1.text = string.format ("asdfadsf {: c} adsfasdf", b); //asdfadsf¥1,234.13adsfasdf
Label2.Text = "asdfadsf"+b.ToString("C")+"adsfasdf"; //asdfadsf¥1,234.13adsfasdf
Label1.Text = string.Format("{:C3}",b); //¥1,234.125
Label2.Text = b.ToString("C3"); //¥1,234.125
Label1.Text = string.Format("{:d}",a); //decimal-12345678
label2.text = b.tostring ("d"); //decimal-same type, conversion error
label1.text = string.format ("{: e}", a); //index-1.234568e+7
label2.text = b.tostring ("e"); //index-1.234125e+3
label1.text = string.format ("{: f}", a); //fixed point-12345678.
label2.text = b.tostring ("f"); //fixed point-1234.13
label1.text = string.format ("{: n}", a); //value-12,345,678.
label2.text = b.tostring ("n"); //value-1,234.13
label1.text = string.format ("{: x}", a); //hexadecimal-bc614e
label2.text = b.tostring ("x"); //16-Cannot convert with decimal, error
label1.text = string.format ("{: g}", a); //General is the most compact-12345678
label2.text = b.tostring ("g"); //Universal is the most compact-1234.12543
label1.text = string.format ("{: r}", a); //Turn around without losing precision-integers are not allowed, and an error is reported
Label2.Text = b.ToString("r "); //turn around without losing accuracy-1234.2543
double b = 4321.448888894
int a = 1234;
custom mode output:
//" "Description: placeholder, and if possible, padding bits
label1.text = string.format ("{: } ",a); // 1234
Label2.Text = string.Format("{:}",b); //4321
/"#" Description: Placeholder, with padding bits
label1.text = string.format ("{: # # # # #}", a) if possible; // 1234
Label2.Text = string.Format("{:#######}",b); // 4321
Label1.Text = string.Format("{:#####}",a); // 1234
Label2.Text = string.Format("{:#}",b); //4321
/"."Description: decimal point
label1.text = string.format ("{: .}", a); //1234.
Label2.Text = string.Format("{:.}",b); //4321.125
double b = 87654321.12543;
int a = 12345678;
// ","Description: digital grouping, also used for multiplier
label1.text = string.format ("{: ,}", a); // 12,345,678
Label2.Text = string.Format("{:,}",b); // 87,654,32
Label1.Text = string.Format("{:,}",a); // 12346
Label2.Text = string.Format("{:,}",b); // 87654
Label1.Text = string.Format("{:,,}",a); // 12
Label2.Text = string.Format("{:,,}",b); // 88
Label1.Text = string.Format("{:,,,}",a); //
Label2.Text = string.Format("{:,,,}",b); //
//"% "Description: the format is percentage
label1.text = string.format ("{: %} ",a); // 12345678%
Label2.Text = string.Format("{:#%}",b); // 8765432113%
Label1.Text = string.Format("{:.%}",a); // 12345678.%
Label2.Text = string.Format("{:#.%}",b); //8765432112.54%
/"ABC" Description: displays the text in single quotation marks
label1.text = string.format ("{:' text' }",a); //text12345678
label2.text = string.format ("{: text}", b); //Text87654321
/""Description: The character to be printed followed by 1 is also used to transfer the character "n
Label1.Text = string.Format (""Hello!" " ""); //"Hello!"
Label2.Text = string.Format("[url=file://""c""books""new""we.asp]""c""books""new""we.asp"); //"c" books "new" we.asp
/"@" Description: followed by the characters to be printed,
Label1.Text = string.Format(@ ""Hello! " ""); //"Hello!" To print ",you need to enter two pairs to
label2.text = string.format (@" "c" books "new" we.asp "); //"c" books "new" we.asp
label1.text = string.format ("{: yyyy year -mm month -dd day", DateTime.Now);
date conversion-
In order to achieve different display effects, we sometimes need to convert the time, and the default format is: 27-1-3 14:33:34. To convert to other formats, we need to use the method of DateTime.ToString (String, IFormatProvider), as shown below:
using System;
using System.Globalization;
String format="D";
DateTime date=DataTime,Now;
Response.Write(date.ToString(format, DateTimeFormatInfo.InvariantInfo));
result output
Thursday, June 16, 25
Detailed usage of parameter format:
Format character association attribute/ Description
d shortdatepattern
d longdatepattern
f complete date and time (long date and short time)
F FullDateTimePattern
g general (short date and short time)
G general (short date and long time)
m, M MonthDayPattern
r、 R RFC1123Pattern
s uses the SortableDateTimePattern of local time (based on ISO 861)
t ShorttimePattern
t Long Time Pattern
u Universal sortable datetime pattern for display. Use the format of time
U Use the complete date and time of universal time (long date and long time)
y, Y YearMonthPattern
The following table lists the patterns that can be combined to construct custom patterns. These patterns are case-sensitive; For example, "MM" is recognized but "mm" is not recognized. If the custom pattern contains blank characters or characters enclosed in single quotes, the output string page will also contain these characters. Characters that are not defined as part of the format pattern or as format characters are copied in their original meanings.
format pattern description
a day in d month. A single-digit date has no leading zero.
a day in p>dd month. A single-digit date has a leading zero.
the abbreviated name of a day in a DDD week, which is defined in AbbreviatedDayNames.
the full name of a day in p>dddd week, which is defined in DayNames.
the number of m months. Single-digit months have no leading zeros.
MM month number. Single-digit months have a leading zero.
the abbreviated name of p>MMM month, which is defined in AbbreviatedMonthNames.
the full name of p>MMMM month, which is defined in MonthNames.
y does not contain the year of the era. If the year without era is less than 1, the year without leading zero is displayed.
yy does not contain the year of the era. If the year without era is less than 1, the year with leading zero is displayed.
yyyy includes the four-digit year of the era.
gg period or era. If the date to be formatted does not have an associated period or era string, the pattern is ignored.
h 12-hour hour. One-digit hours have no leading zeros.
hh 12-hour hour. One-digit hours have leading zeros.
H 24-hour hour. One-digit hours have no leading zeros.
HH 24-hour hour. One-digit hours have leading zeros.
m minutes. Minutes in a single digit have no leading zeros.
mm minutes. Minutes in a single digit have a leading zero.
s seconds. Single-digit seconds have no leading zeros.
ss seconds. One-digit seconds have a leading zero.
the decimal precision of f seconds is one digit. The remaining figures are truncated.
the decimal precision of p>ff seconds is two digits. The remaining figures are truncated.
the fractional precision of p>fff seconds is three digits. The remaining figures are truncated.
the decimal precision of p>ffff seconds is four digits. The remaining figures are truncated.
the decimal precision of p>fffff seconds is five digits. The remaining figures are truncated.
the decimal precision of p>ffffff seconds is six digits. The remaining figures are truncated.
fffffff