ToString格式化

来源:互联网 发布:免费个人域名注册 编辑:程序博客网 时间:2024/06/06 04:57
日常开发中,格式字符串的情况非常多。经常也会忘记,经常去查有些麻烦,所以今天就花点时间做个整理。格式字符串用的比较多的有数字、日期与枚举的格式化。

 

 一、数字格式字符串  

C或c本地货币格式D或d  十进制格式,把整数转换为以10为基数的书,如果给定一个精度说明符,就加上前导0E或e  科学计数法(指数)格式,精度说明符设置小数位数(默认为6),格式字符串的大小写(e或E)确定指数符号的大小写。F或f  固定点格式,精度说明符设置小数位数,可以为0G或g普通格式,使用E或F格式取决于哪种格式较简单N或n  数字格式,用逗号表示千分符,例如32,767.44P或p  百分数格式X或x十六进制格式,精度说明符用于加上前导0

  

  先用例子说明几种格式字符串的方法:

 

[csharp] view plain copy
  1. double d = 123.456;  
  2. Console.WriteLine("ToString:{0}", d.ToString("C"));  
  3. Console.WriteLine("Format:{0}"string.Format("{0:C}",d));  
  4. Console.WriteLine("Console:{0:C}", d);  

输出结果:  

  

  

  数字格式化程序例子:

[csharp] view plain copy
  1. Console.WriteLine("十六进制格式符X:{0}", (145).ToString("X"));//X只支持整型  
  2.  double[] numbers = {1054.32179, -195489100.8377, 1.0437E21,   
  3.               -1.0573e-05};  
  4. string[] specifiers = { "C""E""F""G""N","P",   
  5.               "R","#,000.000""0.###E-000",  
  6.               "000,000,000,000.00###" };  
  7. foreach (double number in numbers)  
  8. {  
  9.     Console.WriteLine("Formatting of {0}:", number);  
  10.     foreach (string specifier in specifiers)  
  11.     {  
  12.         Console.WriteLine("   {0,5}: {1}",  
  13.                   specifier, number.ToString(specifier));  
  14.     }  
  15.     Console.WriteLine();  
  16. }  


输出结果:

  

MSDN:Double.ToString 方法 (String)

 

  二、日期格式字符串 

[csharp] view plain copy
  1. static void DateToString()  
  2. {  
  3.     DateTime dateValue = DateTime.Now;  
  4.     // Create an array of standard format strings.  
  5.     string[] standardFmts = {"d""D""f""F""g""G""m""o",   
  6.                        "R""s""t""T""u""U""y"};  
  7.     // Output date and time using each standard format string.  
  8.     foreach (string standardFmt in standardFmts)  
  9.         Console.WriteLine("{0}: {1}", standardFmt,  
  10.                           dateValue.ToString(standardFmt));  
  11.     Console.WriteLine();  
  12.   
  13.     // Create an array of some custom format strings.  
  14.     string[] customFmts = {"yyyyMMddHHmmss","h:mm:ss.ff t""d MMM yyyy""HH:mm:ss.f",   
  15.                      "dd MMM HH:mm:ss", @"\Mon\t\h\: M""HH:mm:ss.ffffzzz" };  
  16.     // Output date and time using each custom format string.  
  17.     foreach (string customFmt in customFmts)  
  18.         Console.WriteLine("'{0}': {1}", customFmt,  
  19.                           dateValue.ToString(customFmt));  
  20. }  


输出结果:

  

    MSDN:DateTime.ToString 方法 (String)  

 

 

  三、枚举格式字符串 

[csharp] view plain copy
  1. enum Colors { Red, Green, Blue, Yellow = 12 };  
  2.         static void EnumToString()  
  3.         {  
  4.             Colors myColor = Colors.Yellow;  
  5.   
  6.             Console.WriteLine("Colors.Red = {0}", Colors.Red.ToString("d"));  
  7.             Console.WriteLine("Colors.Green = {0}", Colors.Green.ToString("d"));  
  8.             Console.WriteLine("Colors.Blue = {0}", Colors.Blue.ToString("d"));  
  9.             Console.WriteLine("Colors.Yellow = {0}", Colors.Yellow.ToString("d"));  
  10.   
  11.             Console.WriteLine("{0}myColor = Colors.Yellow{0}", Environment.NewLine);  
  12.   
  13.             Console.WriteLine("myColor.ToString(\"g\") = {0}", myColor.ToString("g"));  
  14.             Console.WriteLine("myColor.ToString(\"G\") = {0}", myColor.ToString("G"));  
  15.   
  16.             Console.WriteLine("myColor.ToString(\"x\") = {0}", myColor.ToString("x"));  
  17.             Console.WriteLine("myColor.ToString(\"X\") = {0}", myColor.ToString("X"));  
  18.   
  19.             Console.WriteLine("myColor.ToString(\"d\") = {0}", myColor.ToString("d"));  
  20.             Console.WriteLine("myColor.ToString(\"D\") = {0}", myColor.ToString("D"));  
  21.   
  22.             Console.WriteLine("myColor.ToString(\"f\") = {0}", myColor.ToString("f"));  
  23.             Console.WriteLine("myColor.ToString(\"F\") = {0}", myColor.ToString("F"));  
  24.         }  


输出结果:  

  

转载地址:http://www.cnblogs.com/wangshenhe/archive/2012/06/29/2569629.html

0 0