【整理】C# ToString格式字符串整理(Format)(數字、日期和枚舉的標准格式設置說明符)(SamWang)


  日常開發中,格式字符串的情況非常多。經常也會忘記,經常去查有些麻煩,所以今天就花點時間做個整理。

  格式字符串用的比較多的有數字、日期與枚舉的格式化。

 

 一、數字格式字符串  

C或c 本地貨幣格式
D或d   十進制格式,把整數轉換為以10為基數的書,如果給定一個精度說明符,就加上前導0
E或e   科學計數法(指數)格式,精度說明符設置小數位數(默認為6),格式字符串的大小寫(e或E)確定指數符號的大小寫。
F或f   固定點格式,精度說明符設置小數位數,可以為0
G或g 普通格式,使用E或F格式取決於哪種格式較簡單
N或n   數字格式,用逗號表示千分符,例如32,767.44
P或p   百分數格式
X或x 十六進制格式,精度說明符用於加上前導0

  

  先用例子說明幾種格式字符串的方法:

  

      double d = 123.456;
     Console.WriteLine("ToString:{0}", d.ToString("C"));
     Console.WriteLine("Format:{0}", string.Format("{0:C}",d));
     Console.WriteLine("Console:{0:C}", d);

 

   輸出結果:  

  

  

  數字格式化程序例子:

     Console.WriteLine("十六進制格式符X:{0}", (145).ToString("X"));//X只支持整型
      double[] numbers = {1054.32179, -195489100.8377, 1.0437E21, 
                   -1.0573e-05};
     string[] specifiers = { "C", "E", "F", "G", "N","P", 
                   "R","#,000.000", "0.###E-000",
                   "000,000,000,000.00###" };
     foreach (double number in numbers)
     {
         Console.WriteLine("Formatting of {0}:", number);
         foreach (string specifier in specifiers)
         {
             Console.WriteLine("   {0,5}: {1}",
                       specifier, number.ToString(specifier));
         }
         Console.WriteLine();
     }

  輸出結果:

  

MSDN:Double.ToString 方法 (String)

 

  二、日期格式字符串 

  

        static void DateToString()
        {
            DateTime dateValue = DateTime.Now;
            // Create an array of standard format strings.
            string[] standardFmts = {"d", "D", "f", "F", "g", "G", "m", "o", 
                               "R", "s", "t", "T", "u", "U", "y"};
            // Output date and time using each standard format string.
            foreach (string standardFmt in standardFmts)
                Console.WriteLine("{0}: {1}", standardFmt,
                                  dateValue.ToString(standardFmt));
            Console.WriteLine();

            // Create an array of some custom format strings.
            string[] customFmts = {"yyyyMMddHHmmss","h:mm:ss.ff t", "d MMM yyyy", "HH:mm:ss.f", 
                             "dd MMM HH:mm:ss", @"\Mon\t\h\: M", "HH:mm:ss.ffffzzz" };
            // Output date and time using each custom format string.
            foreach (string customFmt in customFmts)
                Console.WriteLine("'{0}': {1}", customFmt,
                                  dateValue.ToString(customFmt));
        }

  輸出結果:

  

    MSDN:DateTime.ToString 方法 (String)  

 

 

  三、枚舉格式字符串   

        enum Colors { Red, Green, Blue, Yellow = 12 };
        static void EnumToString()
        {
            Colors myColor = Colors.Yellow;

            Console.WriteLine("Colors.Red = {0}", Colors.Red.ToString("d"));
            Console.WriteLine("Colors.Green = {0}", Colors.Green.ToString("d"));
            Console.WriteLine("Colors.Blue = {0}", Colors.Blue.ToString("d"));
            Console.WriteLine("Colors.Yellow = {0}", Colors.Yellow.ToString("d"));

            Console.WriteLine("{0}myColor = Colors.Yellow{0}", Environment.NewLine);

            Console.WriteLine("myColor.ToString(\"g\") = {0}", myColor.ToString("g"));
            Console.WriteLine("myColor.ToString(\"G\") = {0}", myColor.ToString("G"));

            Console.WriteLine("myColor.ToString(\"x\") = {0}", myColor.ToString("x"));
            Console.WriteLine("myColor.ToString(\"X\") = {0}", myColor.ToString("X"));

            Console.WriteLine("myColor.ToString(\"d\") = {0}", myColor.ToString("d"));
            Console.WriteLine("myColor.ToString(\"D\") = {0}", myColor.ToString("D"));

            Console.WriteLine("myColor.ToString(\"f\") = {0}", myColor.ToString("f"));
            Console.WriteLine("myColor.ToString(\"F\") = {0}", myColor.ToString("F"));
        }

  輸出結果:  

  

MSDN:Enum.ToString 方法 (String)

 


作者:SamWang
出處:http://wangshenhe.cnblogs.com/
本文版權歸作者和博客園共有,歡迎圍觀轉載。轉載時請您務必在文章明顯位置給出原文鏈接,謝謝您的合作。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM