C # Formatting examples of digital code in detail

Source: Internet
Author: User
Tags iso 8601 local time truncated
int a = 12345678;//format is sring output Label1.Text = string. Format ("Asdfadsf{0}adsfasdf", a); Label2.Text = "ASDFADSF" +a.tostring () + "ADSFASDF"; Label1.Text = string. Format ("Asdfadsf{0:c}adsfasdf", a);//asdfadsf¥1,234.00adsfasdflabel2.text = "ASDFADSF" +a.tostring ("C") + "ADSFASDF" ;//asdfadsf¥1,234.00adsfasdfdouble B = 1234.12543;int A = 12345678;//format is a special string-style output Label1.Text = string. Format ("Asdfadsf{0:c}adsfasdf", b);//asdfadsf¥1,234.13adsfasdflabel2.text = "ASDFADSF" +b.tostring ("C") + "ADSFASDF" ;//asdfadsf¥1,234.13adsfasdflabel1.text = string. Format ("{0:c3}", b);//¥1,234.125label2.text = b.tostring ("C3");//¥1,234.125label1.text = string. Format ("{0:d}", a);//Decimal--12345678label2.text = b.tostring ("D");//decimal--same type, conversion error Label1.Text = string. Format ("{0:e}", a);//Index--1.234568e+007label2.text = b.tostring ("e");//Index--1.234125e+003label1.text = string. Format ("{0:f}", a);//fixed-point number--12345678.00label2.text = B.tostring ("f");//fixed-point number--1234.13label1.text = String. Format ("{0:n}", a);//Numeric--12,345,678.00label2.text = B.tostring ("n");//Numeric--1,234.13label1.text = string. Format ("{0:x}", a);//Hex--bc614elabel2.text = b.tostring ("x"),//16--with decimal cannot be converted, error Label1.Text = string. Format ("{0:g}", a);//Universal is the most compact--12345678label2.text = B.tostring ("G");//Universal is the most compact--1234.12543label1.text = string. Format ("{0:r}", a);//round-off without loss of precision-integer not allowed, error Label2.Text = b.tostring ("R");//no loss of precision--1234.12543double B = 4321.12543; int a = 1234;

Custom mode output:

"0" description: Placeholder, if possible, fill bit Label1.Text = string. Format ("{0:000000}", a);//001234label2.text = string. Format ("{0:000000}", b);//004321//"#" description: Placeholder, if possible, fill bit Label1.Text = string. Format ("{0:#######}", a);//1234label2.text = string. Format ("{0:#######}", b);//4321label1.text = string. Format ("{0: #0 # #}", a);//01234label2.text = string. Format ("{0:0#0000}", b);//004321//"." Description: Decimal Label1.Text = string. Format ("{0:000.000}", a);//1234.000label2.text = string. Format ("{0:000.000}", b);//4321.125double b = 87654321.12543;int a = 12345678;//"," Description: Number grouping, also used for multiplier Label1.Text = string. Format ("{0:0,00}", a);//12,345,678label2.text = string. Format ("{0:0,00}", b);//87,654,32label1.text = string. Format ("{0:0,}", a);//12346label2.text = string. Format ("{0:0,}", b);//87654label1.text = string. Format ("{0:0,,}", a);//12label2.text = string. Format ("{0:0,,}", b);//88label1.text = string. Format ("{0:0,,,}", a);//0label2.text = string. Format ("{0:0,,,}", b);//0//"%" description: Formatted as percent Label1.Text = string. Format ("{0:0%}", a);//1234567800%label2.Text = string. Format ("{0:#%}", b);//8765432113%label1.text = string. Format ("{0:0.00%}", a);//1234567800.00%label2.text = string. Format ("{0:#.00%}", b);//8765432112.54%//"ABC" description: Displays the text within single quotation marks Label1.Text = string. Format ("{0: ' text ' 0}", a);//Text 12345678label2.text = string. Format ("{0: Text 0}", b);//Text 87654321//"" "Description: followed by 1 characters to print the word, also used for the transfer" 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 character to print the word, 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 ("{0:yyyy year-mm month-dd Day", DateTime.Now);

Date conversion One

In order to achieve different display effects sometimes we need to convert the time, the default format is: 2007-01-03 14:33:34, to convert to another format, to use the DateTime.ToString method (String, IFormatProvider), As shown below:

Using system;using system.globalization; String format= "D";D atetime Date=datatime,now; Response.Write (date. ToString (format, datetimeformatinfo.invariantinfo));

Result output

Thursday, June 16, 2005


Parameter format verbose usage:

Format Character Association properties/Description
D ShortDatePattern
D Longdatepattern
F Full Date and time (long date and short time)
F Fulldatetimepattern (long date and long time)
G General (short date and short time)
G General (short date and long time)
M, M Monthdaypattern
R, R Rfc1123pattern
s use local time Sortabledatetimepattern (based on ISO 8601)
T Shorttimepattern
T Longtimepattern
U universalsortabledatetimepattern format for displaying the Universal Time
U use the full date and time of the universal Time (long date and long time)
Y, y Yearmonthpattern

The following table lists the patterns that can be combined to construct a custom pattern. These patterns are case-sensitive, for example, "MM" is recognized, but "MM" is not recognized. If the custom pattern contains white space characters or characters enclosed in single quotation marks, the output string page will also contain these characters. Characters that are not defined as part of the format pattern or that are not defined as format characters are copied verbatim.

Format Pattern Description
A day in the D month. One-digit date has no leading zeros.
DD a day of the month. A one-digit date has a leading zero.
The abbreviated name of the day of the DDD week, defined in Abbreviateddaynames.
dddd the full name of the day of the week, as defined in DayNames.
M-month number. One-digit month has no leading zeros.
MM month number. One-digit month has a leading zero.
Abbreviated name of the MMM month, defined in AbbreviatedMonthNames.
The full name of the MMMM month, as defined in MonthNames.
Y does not contain the year of the era. If the year that does not contain an era is less than 10, the year is displayed without leading zeros.
YY does not contain the year of the era. If the year that does not contain an era is less than 10, the year with leading zeros is displayed.
The 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 system. One-digit hours do not have leading zeros.
HH 12-hour hour. One-digit hours have leading zeros.
H 24 hour hour system. One-digit hours do not have leading zeros.
HH 24-hour hour. One-digit hours have leading zeros.
M minutes. A single-digit number of minutes does not have a leading zero.
MM minutes. A single-digit number of minutes has a leading zero.
s seconds. The number of seconds in a single digit does not have a leading zero.
SS seconds. The number of seconds of one digit has a leading zero.
The fractional precision of the F-second is one digit. The remaining digits are truncated.
The fractional precision of the FF seconds is two bits. The remaining digits are truncated.
The fractional precision of FFF seconds is three bits. The remaining digits are truncated.
The fractional precision of ffff seconds is four bits. The remaining digits are truncated.
The fractional precision of fffff seconds is five bits. The remaining digits are truncated.
The fractional precision of ffffff seconds is six bits. The remaining digits are truncated.
The fractional precision of fffffff seconds is seven bits. The remaining digits are truncated.
t the am/pm defined in AMDesignator or PMDesignator indicates the first character of the item, if one exists.
The AM/PM indicated by the TT in AMDesignator or PMDesignator (if present).
The Z Time zone offset ("+" or "-" followed by hours only). One-digit hours do not have leading zeros. For example, Pacific Standard Time is "-8".
ZZ Time zone offset ("+" or "-" followed by hours only). One-digit hours have leading zeros. For example, Pacific Standard Time is "-08".
The ZZZ Full time zone offset ("+" or "-" followed by hours and minutes). The number of hours and minutes of a single digit has a leading zero. For example, Pacific Standard Time is " -08:00".
: The default time delimiter defined in TimeSeparator.
/The default date delimiter defined in DateSeparator.
% c where C is the format pattern (if used alone). You can omit the "%" character if the format pattern is merged with literal characters or other format patterns.
"C where c is any character. Displays the characters as they are originally defined. To display a backslash character, use the.

Only the format patterns listed in the second table above can be used to create custom patterns, and the standard format characters listed in the first table cannot be used to create custom patterns. The custom pattern is at least two characters long;

DateTime.ToString ("D") returns a DateTime value; "D" is a standard short date pattern.
DateTime.ToString ("%d") returns the day of the month; "%d" is a custom pattern.
DateTime.ToString ("D") returns the day of the month followed by a blank character, and "D" is a custom pattern.

More convenient is that the above parameters can be arbitrarily combined, and will not be wrong, try more, will definitely find the time format you want
To get the time in this format for June 2005
It can be written like this:
Date. ToString ("yyyy mm month", Datetimeformatinfo.invariantinfo)

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.