This article analyzes the method of formatting numbers in C#. Share it for your reference. The details are as follows:
int a = 12345678; //Format is sring output = ("asdfadsf{0}adsfasdf",a); = "asdfadsf"+()+"adsfasdf"; = ("asdfadsf{0:C}adsfasdf",a);//asdfadsf¥1,234.00adsfasdf = "asdfadsf"+("C")+"adsfasdf";//asdfadsf¥1,234.00adsfasdf double b = 1234.12543; int a = 12345678; //The format is a special string style output = ("asdfadsf{0:C}adsfasdf",b);//asdfadsf¥1,234.13adsfasdf = "asdfadsf"+("C")+"adsfasdf";//asdfadsf¥1,234.13adsfasdf = ("{0:C3}",b);//¥1,234.125 = ("C3");//¥1,234.125 = ("{0:d}",a);//Decimal--12345678 = ("d");//Decimal--Same type, conversion error reported = ("{0:e}",a);//Index--1.234568e+007 = ("e");//Index--1.234125e+003 = ("{0:f}",a);//Fixed point number-12345678.00 = ("f");//Fixed point number-1234.13 = ("{0:n}",a);//Value--12,345,678.00 = ("n");//Value--1,234.13 = ("{0:x}",a);//Hexadecimal--bc614e = ("x");//16--There is no conversion with decimals, an error occurred = ("{0:g}",a);//Universal is the most compact--12345678 = ("g");//General-common is the most compact-1234.12543 = ("{0:r}",a);//Winning and turning without losing accuracy - integers are not allowed, and an error is reported = ("r");//Write it without losing accuracy-1234.12543 double b = 4321.12543; int a = 1234;
Custom mode output:
//"0" description: placeholder, fill bit if possible, if possible = ("{0:000000}",a);// 001234 = ("{0:000000}",b);// 004321 //"#"Description: Placeholder, if possible, fill the bit = ("{0:#######}",a);// 1234 = ("{0:#######}",b);// 4321 = ("{0:#0####}",a);// 01234 = ("{0:0#0000}",b);// 004321 //"." Description: decimal point = ("{0:000.000}",a);//1234.000 = ("{0:000.000}",b);//4321.125 double b = 87654321.12543; int a = 12345678; //"," Description: Digital packets, also used for multiplier = ("{0:0,00}",a);// 12,345,678 = ("{0:0,00}",b);// 87,654,32 = ("{0:0,}",a);// 12346 = ("{0:0,}",b);// 87654 = ("{0:0,,}",a);// 12 = ("{0:0,,}",b);// 88 = ("{0:0,,,}",a);// 0 = ("{0:0,,,}",b);// 0 //"%" description: format is percentage = ("{0:0%}",a);// 1234567800% = ("{0:#%}",b);// 8765432113% = ("{0:0.00%}",a);// 1234567800.00% = ("{0:#.00%}",b);// 8765432112.54% //"abc" description: display text in single quotes = ("{0:'text'0}",a);// Text 12345678 = ("{0:text0}",b);// Text 87654321 //"""Description: The character followed by 1 to print the word, also used to transfer the character "n, etc. = ("""Hello!""");// "Hello!" = ("[url=file://""c""books""new""]""c""books""new""");//"c"books"new" //"@" description: followed by the character to be printed, = (@"""Hello!"""); // "Hello!" To print", you need to enter two pairs to = (@""c"books"new"");//"c"books"new" = ("{0:yyyyYear-mmmoon-ddday",);
Date conversion:
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 it to other formats, the method to be used (String, IFormatProvider) is as follows:
using System; using ; String format="D"; DateTime date=DataTime,Now; ((format, ));
Results output
Thursday, June 16, 2005
Detailed usage of parameter format:
Format Characters Associated Attributes/Descriptions
d ShortDatePattern
D LongDatePattern
f Full date and time (long date and short time)
F FullDateTimePattern (Long date and long time)
g Regular (short date and short time)
G Regular (short date and long time)
m、M MonthDayPattern
r、R RFC1123Pattern
s SortableDateTimePattern using local time (based on ISO 8601)
t ShortTimePattern
T LongTimePattern
u UniversalSortableDateTimePattern Format for displaying common time
U Full date and time (long date and long time) using common time
y、Y YearMonthPattern
The following table lists the patterns that can be merged to construct custom patterns. These patterns are case sensitive; for example, identify "MM", but not "mm". If the custom pattern contains whitespace characters or characters enclosed in single quotes, the output string page will also contain these characters. Characters not defined as part of the format pattern or not defined as format characters are copied in their original meaning.
Format Mode Description
d One day in the month. A single-digit date has no leading zeros.
dd One day in the month. A single-digit date has a leading zero.
ddd The abbreviation name for a day of the week, defined in AbbreviatedDayNames.
dddd The full name of a day of the week, defined in DayNames.
M Month figure. A single-digit month has no leading zeros.
MM Month figures. A single-digit month has a leading zero.
The abbreviation name of the MMM month, defined in AbbreviatedMonthNames.
The full name of the MMMM month, defined in MonthNames.
y does not contain the year of the epoch. If the year that does not contain an epoch is less than 10, a year without leading zeros is displayed.
yy does not contain the year of the epoch. If the year without an epoch is less than 10, the year with leading zeros is displayed.
yyyy includes four-digit year of the epoch.
gg period or epoch. If the date you want to format does not have an associated epoch or epoch string, this pattern is ignored.
h 12-hour hours. A single-digit hour number has no leading zeros.
hh 12-hour hours. The number of hours in a single digit has leading zeros.
H 24-hour hours. A single-digit hour number has no leading zeros.
HH 24-hour hours. The number of hours in a single digit has leading zeros.
m minutes. A single-digit minute number has no leading zeros.
mm minutes. A single-digit minute number has a leading zero.
s seconds. The number of seconds in a single digit has no leading zeros.
ss seconds. A single-digit second has a leading zero.
The decimal accuracy of f seconds is one bit. The rest of the numbers are truncated.
The decimal accuracy of ff seconds is two digits. The rest of the numbers are truncated.
The decimal accuracy of fff seconds is three digits. The rest of the numbers are truncated.
ffff The decimal accuracy of the second is four digits. The rest of the numbers are truncated.
ffffff The decimal accuracy of a second is five digits. The rest of the numbers are truncated.
ffffff The decimal accuracy of a second is six digits. The rest of the numbers are truncated.
ffffffff The decimal accuracy of a second is seven digits. The rest of the numbers are truncated.
t The first character (if present) of the AM/PM indicator defined in AMDesignator or PMDesignator.
tt The AM/PM indicator defined in AMDesignator or PMDesignator (if present).
z Time zone offset ("+" or "-" is followed only by hours). A single-digit hour number has no leading zeros. For example, Pacific Standard Time is "-8".
zz Time zone offset ("+" or "-" is followed only by hours). The number of hours in a single digit has leading zeros. For example, Pacific Standard Time is "-08".
zzz Full time zone offset ("+" or "-" followed by hours and minutes). The number of hours and minutes in a single digit has leading zeros. For example, Pacific Standard Time is "-08:00".
: The default time separator defined in TimeSeparator.
/ The default date separator defined in DateSeparator.
%c where c is format mode (if used alone). If the format pattern is merged with the primitive characters or other format pattern, the "%" character can be omitted.
" c Where c is any character. Display characters in the original meaning. To display backslash characters, use """.
Only the format schema listed in the second table above can be used to create a custom schema; the standard format characters listed in the first table cannot be used to create a custom schema. The length of the custom pattern is at least two characters; for example,
( "d") Returns the DateTime value; "d" is the standard short date mode.
( "%d") Returns a day in the month; "%d" is a custom mode.
( "d ") Returns a day in the month followed by a blank character; "d" is a custom mode.
What is more convenient is that the above parameters can be combined at will and there will be no errors. Try more and you will definitely find the time format you want.
If you want to get the time of this format in June 2005
You can write this way:
("yyyyy year MM month", )
I hope this article will be helpful to everyone's C# programming.