• ToString()用法大全


    字符型转换为字符串
    
    1.// C 货币 
    2.2.5.ToString("C"); // ¥2.50 
    3.// D 10进制数 
    4.25.ToString("D5"); // 25000 
    5.// E 科学型 
    6.25000.ToString("E"); // 2.500000E+005 
    7.// F 固定点 
    8.25.ToString("F2"); // 25.00 
    9.// G 常规 
    10.2.5.ToString("G"); // 2.5 
    11.// N 数字 
    12.2500000.ToString("N"); // 2,500,000.00  或 1234567.ToString("#,###");
    13.// X 16进制 
    14.255.ToString("X"); // FF 
     
    
    1.// C# 日期格式 
    2.DateTime dt = DateTime.Now; 
    3.  
    4.dt.ToString();//2005-11-5 13:21:25 
    5.dt.ToFileTime().ToString();//127756416859912816 
    6.dt.ToFileTimeUtc().ToString();//127756704859912816 
    7.dt.ToLocalTime().ToString();//2005-11-5 21:21:25 
    8.dt.ToLongDateString().ToString();//2005年11月5日 
    9.dt.ToLongTimeString().ToString();//13:21:25 
    10.dt.ToOADate().ToString();//38661.5565508218 
    11.dt.ToShortDateString().ToString();//2005-11-5 
    12.dt.ToShortTimeString().ToString();//13:21 
    13.dt.ToUniversalTime().ToString();//2005-11-5 5:21:25 
    14.dt.Year.ToString();//2005 
    15.dt.Date.ToString();//2005-11-5 0:00:00 
    16.dt.DayOfWeek.ToString();//Saturday 
    17.dt.DayOfYear.ToString();//309 
    18.dt.Hour.ToString();//13 
    19.dt.Millisecond.ToString();//441 
    20.dt.Minute.ToString();//30 
    21.dt.Month.ToString();//11 
    22.dt.Second.ToString();//28 
    23.dt.Ticks.ToString();//632667942284412864 
    24.dt.TimeOfDay.ToString();//13:30:28.4412864 
    25.dt.ToString();//2005-11-5 13:47:04 
    26.dt.AddYears(1).ToString();//2006-11-5 13:47:04 
    27.dt.AddDays(1.1).ToString();//2005-11-6 16:11:04 
    28.dt.AddHours(1.1).ToString();//2005-11-5 14:53:04 
    29.dt.AddMilliseconds(1.1).ToString();//2005-11-5 13:47:04 
    30.dt.AddMonths(1).ToString();//2005-12-5 13:47:04 
    31.dt.AddSeconds(1.1).ToString();//2005-11-5 13:47:05 
    32.dt.AddMinutes(1.1).ToString();//2005-11-5 13:48:10 
    33.dt.AddTicks(1000).ToString();//2005-11-5 13:47:04 
    34.dt.CompareTo(dt).ToString();//0 
    35.dt.Add(?).ToString();//问号为一个时间段 
    36.dt.Equals("2005-11-6 16:11:04").ToString();//False 
    37.dt.Equals(dt).ToString();//True 
    38.dt.GetHashCode().ToString();//1474088234 
    39.dt.GetType().ToString();//System.DateTime 
    40.dt.GetTypeCode().ToString();//DateTime 
    41.   
    42.dt.GetDateTimeFormats('s')[0].ToString();//2005-11-05T14:06:25 
    43.dt.GetDateTimeFormats('t')[0].ToString();//14:06 
    44.dt.GetDateTimeFormats('y')[0].ToString();//2005年11月 
    45.dt.GetDateTimeFormats('D')[0].ToString();//2005年11月5日 
    46.dt.GetDateTimeFormats('D')[1].ToString();//2005 11 05 
    47.dt.GetDateTimeFormats('D')[2].ToString();//星期六 2005 11 05 
    48.dt.GetDateTimeFormats('D')[3].ToString();//星期六 2005年11月5日 
    49.dt.GetDateTimeFormats('M')[0].ToString();//11月5日 
    50.dt.GetDateTimeFormats('f')[0].ToString();//2005年11月5日 14:06 
    
    51.dt.GetDateTimeFormats('g')[0].ToString();//2005-11-5 14:06 
    
    52.dt.GetDateTimeFormats('r')[0].ToString();//Sat, 05 Nov 2005 14:06:25 GMT 
    53.  
    54.string.Format("{0:d}",dt);//2005-11-5 
    55.string.Format("{0:D}",dt);//2005年11月5日 
    56.string.Format("{0:f}",dt);//2005年11月5日 14:23 
    57.string.Format("{0:F}",dt);//2005年11月5日 14:23:23 
    58.string.Format("{0:g}",dt);//2005-11-5 14:23 
    59.string.Format("{0:G}",dt);//2005-11-5 14:23:23 
    60.string.Format("{0:M}",dt);//11月5日 
    61.string.Format("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT 
    62.string.Format("{0:s}",dt);//2005-11-05T14:23:23 
    63.string.Format("{0:t}",dt);//14:23 
    64.string.Format("{0:T}",dt);//14:23:23 
    65.string.Format("{0:u}",dt);//2005-11-05 14:23:23Z 
    66.string.Format("{0:U}",dt);//2005年11月5日 6:23:23 
    67.string.Format("{0:Y}",dt);//2005年11月 
    68.string.Format("{0}",dt);//2005-11-5 14:23:23 
    
    69.string.Format("{0:yyyyMMddHHmmssffff}",dt); 
    70.  
    71.  
    72.// 计算2个日期之间的天数差 
    73.DateTime dt1 = Convert.DateTime("2007-8-1");    
    
    74.DateTime dt2 = Convert.DateTime("2007-8-15");   
    
    75.TimeSpan span = dt2.Subtract(dt1);             
    
    76.int dayDiff = span.Days + 1;                    
    77.  
    78.// 计算某年某月的天数 
    79.int days = DateTime.DaysInMonth(2007, 8);       
    
    80.days = 31;                                      
    81.  
    82.// 给日期增加一天、减少一天 
    83.DateTime dt =DateTime.Now; 
    84.dt.AddDays(1); //增加一天 
    85.dt.AddDays(-1);//减少一天 
    日期格式模式 说明
    d 月中的某一天。一位数的日期没有前导零。
    dd 月中的某一天。一位数的日期有一个前导零。
    ddd 周中某天的缩写名称,在 AbbreviatedDayNames 中定义。
    dddd 周中某天的完整名称,在 DayNames 中定义。
    M 月份数字。一位数的月份没有前导零。
    MM 月份数字。一位数的月份有一个前导零。
    MMM 月份的缩写名称,在 AbbreviatedMonthNames 中定义。
    MMMM 月份的完整名称,在 MonthNames 中定义。
    y 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示不具有前导零的年份。
    yy 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。
    yyyy 包括纪元的四位数的年份。
    gg 时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。
    h 12 小时制的小时。一位数的小时数没有前导零。
    hh 12 小时制的小时。一位数的小时数有前导零。
    H 24 小时制的小时。一位数的小时数没有前导零。
    HH 24 小时制的小时。一位数的小时数有前导零。
    m 分钟。一位数的分钟数没有前导零。
    mm 分钟。一位数的分钟数有一个前导零。
    s 秒。一位数的秒数没有前导零。
    ss 秒。一位数的秒数有一个前导零。
    f 秒的小数精度为一位。其余数字被截断。
    ff 秒的小数精度为两位。其余数字被截断。
    fff 秒的小数精度为三位。其余数字被截断。
    ffff 秒的小数精度为四位。其余数字被截断。
    fffff 秒的小数精度为五位。其余数字被截断。
    ffffff 秒的小数精度为六位。其余数字被截断。
    fffffff 秒的小数精度为七位。其余数字被截断。
    t 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项的第一个字符(如果存在)。
    tt 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项(如果存在)。
    z 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数没有前导零。例如,太平洋标准时间是“-8”。
    zz 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数有前导零。例如,太平洋标准时间是“-08”。
    zzz 完整时区偏移量(“+”或“-”后面跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-08:00”。
    : 在 TimeSeparator 中定义的默认时间分隔符。
    / 在 DateSeparator 中定义的默认日期分隔符。
    % c 其中 c 是格式模式(如果单独使用)。如果格式模式与原义字符或其他格式模式合并,则可以省略“%”字符。
    / c 其中 c 是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“//”。
    
    只有上面第二个表中列出的格式模式才能用于创建自定义模式;在第一个表中列出的标准格式字符不能用于创建自定义模式。
    自定义模式的长度至少为两个字符;例如,
    
    1.DateTime.ToString( "d"); // 返回 DateTime 值;“d”是标准短日期模式。 
    2.DateTime.ToString( "%d"); // 返回月中的某天;“%d”是自定义模式。 
    3.DateTime.ToString( "d "); // 返回后面跟有一个空白字符的月中的某天;“d”是自定义模式。 
     
    
     
    
    
    --------------------------------------------------------------------------------
    
     
    
     
    
    C
    
     
      
    
    货币
    
     
      
    
    2.5.ToString("C")
    
     
      
    
    ¥2.50 
    
     
     
     
    
    D
    
     
      
    
    十进制数
    
     
      
    
    25.ToString("D5")
    
     
      
    
    00025
    
     
     
     
    
    E
    
     
      
    
    科学型
    
     
      
    
    25000.ToString("E")
    
     
      
    
    2.500000E+005
    
     
     
     
    
    F
    
     
      
    
    固定点
    
     
      
    
    25.ToString("F2")
    
     
      
    
    25.00
    
     
     
     
    
    G
    
     
      
    
    常规
    
     
      
    
    2.5.ToString("G")
    
     
      
    
    2.5
    
     
     
     
    
    N
    
     
      
    
    数字
    
     
      
    
    2500000.ToString("N")
    
     
      
    
    2,500,000.00
    
     
     
     
    
    X
    
     
      
    
    十六进制
    
     
      
    
    255.ToString("X")
    
     
      
    
    FF
    
     
     
    
    formatCode 是可选的格式化代码字符串。(详细内容请搜索“格式化字符串”查看) 
    
    必须用“{”和“}”将格式与其他字符分开。如果恰好在格式中也要使用大括号,可以用连续的两个大括号表示一个大括号,即: “{{”或者“}}”。
    
    常用格式举例:
    
    (1int i=12345; 
    
    this.textBox1.Text=i.ToString();
    
    //结果 12345(this指当前对象,或叫当前类的实例)
    
    this.textBox2.Text=i.ToString("d8"); 
    
    //结果 00012345
    2int 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 ;
    
    //结果 the value is 123
    
    this.textBox2.Text=s2;
    
    //结果 the value is 123.450 
    3double i=12345.6789;
    
    this.textBox1.Text=i.ToString("f2"); //结果 12345.68
    
    this.textBox2.Text=i.ToString("f6"); 
    
    //结果 12345.678900
    4double i=12345.6789;
    
    this.textBox1.Text=i.ToString("n"); //结果 12,345.68
    
    this.textBox2.Text=i.ToString(“n4”); //结果 12,345.6789 
    5double i=0.126;
    
    string s=string.Format("the value is {0:p}",i);
    
    this.textBox1.Text=i.ToString("p"); //结果 12.6%
    
    this.textBox2.Text=s; //结果 the value is 12.6% 
    6) DateTime dt =new DateTime(2003,5,25); 
    
    this.textBox1.Text=dt.ToString("yy.M.d");
    
    //结果 03.5.25
    
    this.textBox2.Text=dt.ToString(“yyyy年M月”);
    
    //结果 2003年5月
    
    Convert.ToDateTime("2005/12/22 22:22:22").ToString("yyyy/MM/dd HH:mm:ss")
    "2005/12/22 22:22:22"7int i=123;
    
    double j=123.45;
    
    string s=string.Format("i:{0,-7},j:{1,7}",i,j); 
    
    //-7表示左对齐,占7位
    
    this.textBox1.Text=s ; 
    
    //结果i:123 ,j: 123.45  
  • 相关阅读:
    106. Construct Binary Tree from Inorder and Postorder Traversal
    105. Construct Binary Tree from Preorder and Inorder Traversal
    449. Serialize and Deserialize BST
    114. Flatten Binary Tree to Linked List
    199. Binary Tree Right Side View
    173. Binary Search Tree Iterator
    98. Validate Binary Search Tree
    965. Univalued Binary Tree
    589. N-ary Tree Preorder Traversal
    eclipse设置总结
  • 原文地址:https://www.cnblogs.com/lyghost/p/2606726.html
Copyright © 2020-2023  润新知