zoukankan      html  css  js  c++  java
  • js字符串的各种格式转换

    平时我们经常会需要将字符转换为各种不同的格式,例如钱:0元需要转换为0.00显示;需要转换为16进制显示的数,这样的例子有很多,平时在应用中也是用得较多... 如果我们都计算出对的格式,然后再显示的话,显然浪费代码和效率,今天看见了许多ToString可以解决的格式,总结给大家,希望可以方便大家。

    平时我们经常会需要将字符转换为各种不同的格式,例如钱:0元需要转换为0.00显示;需要转换为16进制显示的数,这样的例子有很多,平时在应用中也是用得较多...

    如果我们都计算出对的格式,然后再显示的话,显然浪费代码和效率,今天看见了许多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进制数字 


    7.日期格式的转换
    日期格式初了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位

    暂时就先写这么多,如果以后有发现会继续修改

  • 相关阅读:
    Python for i 循环
    Python 输入分数并评
    用户名和密码的输入
    cocos2d-x 3.0学习
    VS2008 ShotKey
    Cocos2d-x 3.0的安装方法
    VFC
    一、在WIN7 64位系统平台,VS2013环境下安装WTL90_4090_RC1(2014-04-01)
    http://www.vcf-online.org/
    Win7 64位 VS2012 安装 Qt5
  • 原文地址:https://www.cnblogs.com/mfryf/p/2554222.html
Copyright © 2011-2022 走看看