Home > Article > Web Front-end > Conversion of various formats of js string ToString, Format_javascript skills
If we all calculate the correct format and then display it, it will obviously be a waste of code and efficiency. Today I saw many formats that ToString can solve. I will summarize them for everyone, hoping it can be convenient for everyone.
1. Convert money format, only int type, float type, double type
double d = 400;
d.ToString("C"); //¥400.00
2.10 decimal Numbers, only int type numbers
int i=400;
i.ToString("D5"); // 00400
3. Scientific numbers, only int type, float type, double type
float f = 400;
f.ToString("E");//4.000000E 002
4. Fixed format numbers, only int type, float type, double type
int i =400;
i.ToString("F3");//400.000 Fn represents n digits after the decimal point, F2 and F represent 2 digits after the decimal point
5.N numeric type
400000000000.ToString("N ")// 400,000,000,000.00" N will convert the number to oh digits after the decimal point, and there will be one every 3 digits.
The difference between it and C is that there is no preceding ¥ symbol
6.16 hexadecimal
400000000000. ToString("x")//"5d21dba000" Convert numbers to hexadecimal numbers
================== Date format conversion====== ==============
Date format In addition to the classes already encapsulated by Datetime, you can also use string .Format(); to convert to a specified format
string .Format("{0:f}",System.DateTime.Now);//Thursday, August 4, 2011 11:23
string.Format("{0:F}", System.DateTime.Now );//Thursday, August 4, 2011 11:23:53
dt.GetDateTimeFormats('s')[0].ToString();//2005-11-05T14:06:25
dt .GetDateTimeFormats('t')[0].ToString();//14:06
dt.GetDateTimeFormats('y')[0].ToString();//November 2005
dt. GetDateTimeFormats('D')[0].ToString();//November 5, 2005
dt.GetDateTimeFormats('D')[1].ToString();//2005 11 05
dt .GetDateTimeFormats('D')[2].ToString();//Saturday 2005 11 05
dt.GetDateTimeFormats('D')[3].ToString();//Saturday November 5, 2005
dt.GetDateTimeFormats('M')[0].ToString();//November 5
dt.GetDateTimeFormats('f')[0].ToString();//November 5, 2005 Day 14:06
dt.GetDateTimeFormats('g')[0].ToString();//2005-11-5 14:06
dt.GetDateTimeFormats('r')[0].ToString( );//Sat, 05 Nov 2005 14:06:25 GMT
string.Format("{0:d}",dt);//2005-11-5
string.Format( "{0:D}",dt);//November 5, 2005
string.Format("{0:f}",dt);//November 5, 2005 14:23
string.Format("{0:F}",dt);//November 5, 2005 14:23:23
string.Format("{0:g}",dt);//2005 -11-5 14:23
string.Format("{0:G}",dt);//2005-11-5 14:23:23
string.Format("{0:M} ",dt);//November 5
string.Format("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT
string.Format(" {0:s}",dt);//2005-11-05T14:23:23
string.Format("{0:t}",dt);//14:23
string.Format ("{0:T}",dt);//14:23:23
string.Format("{0:u}",dt);//2005-11-05 14:23:23Z
string.Format("{0:U}",dt);//November 5, 2005 6:23:23
string.Format("{0:Y}",dt);// November 2005
string.Format("{0}",dt);//2005-11-5 14:23:23
string.Format("{0:yyyyMMddHHmmssffff}", System.DateTime.Now);
yyyy represents the year MM represents the month dd represents the day HH represents the hour mm represents the minute ss represents the second ffff represents the second with 4 decimal places
Write so much for now, if I will continue to modify it if I find anything in the future.