js字串的各種格式的轉換 ToString,Format_javascript技巧

來源:互聯網
上載者:User
如果我們都計算出對的格式,然後再顯示的話,顯然浪費代碼和效率,今天看見了許多ToString可以解決的格式,總結給大家,希望可以方便大家。
1.轉換錢的格式,僅限int型,float型,double型
double d = 400;
d.ToString("C"); //¥400.00
2.10進位數,僅限int型的數字
int i=400;
i.ToString("D5"); // 00400
3.科學型數字,僅限int型,float型,double型
float f = 400;
f.ToString("E");//4.000000E+002
4.固定格式型數字,僅限int型,float型,double型
int i=400;
i.ToString("F3");//400.000 Fn表示小數點後n位,F2和F表示小數點後2位
5.N數字型
400000000000.ToString("N")// 400,000,000,000.00" N會將數字轉換為小數點後噢位,且每隔3位有一個,
它和C的區別是沒有前面的¥符號
6.16進位
400000000000.ToString("x")//"5d21dba000" 將數字轉換為16進位數字
==================日期格式的轉換====================
日期格式初了Datetime已經封裝好了的類之外,還可以用string .Format();來轉換為指定的格式
string.Format("{0:f}",System.DateTime.Now);// 2011年8月4日星期四 11:23
string.Format("{0:F}", System.DateTime.Now);//2011年8月4日星期四 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();//2005年11月
dt.GetDateTimeFormats('D')[0].ToString();//2005年11月5日
dt.GetDateTimeFormats('D')[1].ToString();//2005 11 05
dt.GetDateTimeFormats('D')[2].ToString();//星期六 2005 11 05
dt.GetDateTimeFormats('D')[3].ToString();//星期六 2005年11月5日
dt.GetDateTimeFormats('M')[0].ToString();//11月5日
dt.GetDateTimeFormats('f')[0].ToString();//2005年11月5日 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);//2005年11月5日
string.Format("{0:f}",dt);//2005年11月5日 14:23
string.Format("{0:F}",dt);//2005年11月5日 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);//11月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);//2005年11月5日 6:23:23
string.Format("{0:Y}",dt);//2005年11月
string.Format("{0}",dt);//2005-11-5 14:23:23

string.Format("{0:yyyyMMddHHmmssffff}", System.DateTime.Now);
yyyy表示年 MM表示月 dd表示日 HH表示時 mm表示分 ss表示秒 ffff表示秒的小數為4位

暫時就先寫這麼多,如果以後有發現會繼續修改

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.