ToString of USES methods to summarize of c

  • 2020-05-17 05:16:14
  • OfStack

ToString() usage method summary (C#)
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

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 ;
// 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 "); / / 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; // the result is 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(" M month yyyy year ");
// 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
DateTime.ToString()
We often encounter time conversion to achieve different display effects, the default format is :2006-6-6 14:33:34
What if you want to change it to 2006, 06-2006,2006-6-6 or more?
Here we will use the method DateTime.ToString (String, IFormatProvider).
Example:
using System;
using System.Globalization;
String format="D";
DateTime date=DataTime.Now;
Response.Write(date.ToString(format, DateTimeFormatInfo.InvariantInfo));
Results output
Thursday, June 16, 2006

Detailed usage of the parameter format format is listed here
=======================
Format character association properties/descriptions
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 USES SortableDateTimePattern local time (based on ISO 8601)
t ShortTimePattern
T LongTimePattern
u UniversalSortableDateTimePattern is a format for displaying common times
Full date and time (long date and long time) for U using common time
y, Y YearMonthPattern
The following table lists patterns that can be combined to construct custom patterns
========================================
These patterns are case sensitive; For example, identify "MM" but not "mm". If the custom pattern contains white space characters or characters enclosed in single quotes, the output string page will also contain these characters. A character that is not defined as part 1 of a format pattern or as a format character is copied in its original meaning.
Format mode description:
A day in the month d. A 1-digit date has no leading zero.
A day in the month dd. A 1-digit date has a leading zero.
An abbreviated name for a day in the week of ddd, defined in AbbreviatedDayNames.
The full name of a day in the week of dddd, defined in DayNames.
M monthly figures. One-digit months have no leading zeros.
MM monthly figures. A 1-digit month has a leading zero.
The abbreviated name of the month MMM, as defined in AbbreviatedMonthNames.
The full name of the month MMMM, as defined in MonthNames.
y does not include epoch years. If the year without epoch is less than 10, the year without leading zero is displayed.
yy does not include epoch years. If the year without epoch is less than 10, the year with leading zero is displayed.
yyyy includes the 4-digit years of the era.
gg period or epoch. If you want to format a date that does not have an associated period or epoch string, you ignore the pattern.
h 12-hour system. The number of one-digit hours has no leading zero.
hh 12-hour system. The number of one-digit hours has a leading zero.
H 24-hour hours. The number of one-digit hours has no leading zero.
HH 24-hour hours. The number of one-digit hours has a leading zero.
m minutes. There is no leading zero for the number of minutes of one digit.
mm minutes. The number of minutes in a 1-digit number has a leading zero.
s seconds. The number of seconds in a 1-digit number has no leading zero.
ss seconds. The number of seconds in a 1-digit number has a leading zero.
The decimal precision of f seconds is 1 bit. The remaining Numbers are truncated.
ff seconds has a decimal precision of two digits. The remaining Numbers are truncated.
fff seconds has a decimal precision of 3 digits. The remaining Numbers are truncated.
ffff seconds has a decimal precision of 4 digits. The remaining Numbers are truncated.
fffff seconds has a decimal precision of 5 digits. The remaining Numbers are truncated.
ffffff seconds has a decimal precision of 6 digits. The remaining Numbers are truncated.
fffffff seconds has a decimal precision of 7 digits. The remaining Numbers are truncated.
The first character (if present) of the AM/PM indicator defined in AMDesignator or PMDesignator.
tt the AM/PM indicator, if present, defined in AMDesignator or PMDesignator.
z time zone offset ("+" or "-" followed only by hours). The number of one-digit hours has no leading zero. For example, Pacific standard time is "-8".
zz time zone offset ("+" or "-" followed only by hours). The number of one-digit hours has a leading zero. For example, Pacific standard time is "-08".
zzz full time zone offset ("+" or "-" followed by hours and minutes). The number of hours and minutes of one 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). If the format pattern is combined with a literal character or another format pattern, the "%" character can be omitted.
\ c where c is an arbitrary character. Display characters as they appear. To display backslash characters, use "\\".
Only the format schema listed in table 2 above can be used to create a custom schema; The standard format characters listed in table 1 cannot be used to create custom schemas. The length of a custom pattern is at least two characters. For example,
DateTime.ToString ("d") returns the DateTime value;" d" is the standard short date mode.
DateTime. ToString("%d") returns to a day in the month;" %d" is a custom pattern.
DateTime. ToString("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 will not be wrong, try more, you will find the time format you want
If you want to get June 2005 such format of time
You could write it like this:
date.ToString("yyyy year MM month ", DateTimeFormatInfo.InvariantInfo)
And so on.
Here is a list of 1 specific date formatting usage in Asp.net:
============================================
1. Format date method when binding:

2. Data controls such as DataGrid/DataList, etc.
e.Item.Cell[0].Text = Convert.ToDateTime(e.Item.Cell[0].Text).ToShortDateString();
3. Convert the date display format with class String:
String.Format( "yyyy-MM-dd ",yourDateTime);
4. Convert the date display format with Convert method:
Convert.ToDateTime("2005-8-23").ToString
(" yyMMdd, "System. Globalization. DateTimeFormatInfo. InvariantInfo); // support traditional database
5. Directly convert the date display format with ToString method:
DateTime.Now.ToString("yyyyMMddhhmmss");
DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss")
6. Only the date is shown
DataBinder.Eval(Container.DataItem,"starttime","{0:yyyy-M}")
7. Display all parts of the time, including
DataFormatString='{0:yyyy-MM-dd HH24:mm:ss}' >
Output dates in different formats with DateTime.ToString (string format)

The DateTime.ToString () function has four overloads. The one that is used a lot is the one with no parameters. However, DateTime.ToString (string format) is even more powerful and can output dates in different formats. Here is a list of 1 cases for your reference. Some of them are not listed on MSDN.
1. y represents the year, note that the lowercase y, and the uppercase Y does not represent the year.
2. M stands for month.
3. d stands for date. Note that D does not stand for anything.
4. h or H is for hours, h is for 12 hours, H is for 24 hours.
5. m for minutes.
6. s for seconds. Notice that S doesn't mean anything.
format
The output
The sample
years
y
7
string yy = DateTime.Now.ToString("y-MM")
yy="7-05"
yy
07
string yy = DateTime.Now.ToString("yy-MM")
yy="07-05"
yyy or more y
1984
string yy = DateTime.Now.ToString("yyyy");
yy="2007"
month
M
5.
string mon = DateTime.Parse("1984-05-09")ToString("yyyy-M")
mon = "1984-5"
MM
05.
string mon = DateTime. Parse ToString (" 1984-05-09 ") (" MM ")
mon = "05"
MMM
If it is a Chinese version of the operating system, it will output: may.
If it is an English operating system, enter May for the first three letters of the month
string mon = DateTime.Parse("2006-07-01").ToString("MMM")
English version of operating system: Jul
Chinese version of operating system: July
MMMM or more M
If it is a Chinese version of the operating system, it will output: may.
If the operating system is in English, enter the full write of the month
string mon = DateTime.Parse("2006-07-01").ToString("MMM")
English operating system: July
Chinese version of operating system: July
Date or week
d
9
string dd= DateTime.Parse("1984-05-09")ToString("d")
dd= "9"
dd
09
string dd= DateTime.Parse("1984-05-09")ToString("dd")
dd= "09"
ddd
If it is a Chinese version of the operating system, it will output week, such as week 3. .
If it is an English operating system, the output week abbreviation: such as
Wed
string dd = DateTime.Parse("2006-07-01").ToString("ddd")
English version of operating system: Wed
Chinese version of operating system: Wednesday
dddd or more d
If it is a Chinese version of the operating system, it will output week, such as week 3. .
If it is an English operating system, then output week: such as
Wednesday
string dd = DateTime.Parse("2006-07-01").ToString("dddd")
English version of operating system: Wednesday
Chinese version of operating system: Wednesday
hours
h
Hour range: 1-12
string hh = DateTime.Now.ToString("h");
hh = 8
hh or more h
Hour range: 1-12
string hh = DateTime.Now.ToString("hh");
hh = 08
H
Hour range: 0-23
string hh = DateTime.Now.ToString("yyyy-H");
hh = 2006-8
HH or more H
Hour range: 0-23
string hh = DateTime.Now.ToString("yyyy-HH");
hh = 2006-08
string hh = DateTime.Pare("2006-7-4 18:00:00").ToString("yyyy-HH");
hh = 2006-18
minutes
m
6
string mm = DateTime.Now.ToString("yyyy-MM-dd-m");
mm = "2006-07-01-6";
mm or more m
06
string mm = DateTime.Now.ToString("yyyy-MM-dd-mm");
mm = "2006-07-01-06";
seconds

6
string mm = DateTime.Now.ToString("yyyy-MM-dd-s");
mm = "2006-07-01-6";
ss or more s
06
string mm = DateTime.Now.ToString("yyyy-MM-dd-ss");
mm = "2006-07-01-06";

Related articles: