分享

C# 自定义格式化字符串Console.WriteLine,Console....

 昵称2773297 2010-08-17
//  This  project  illustrates  basic  I/O
//  with  the  console  and  .NET  string  formatting.
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
//自定义格式化字符串
namespace  BasicConsoleIO
{
    class  Program
    {
        static  void  Main(string[]  args)
        {
            Console.WriteLine("*****  Basic  Console  I/O  *****");
            GetUserData();
            Console.WriteLine();
            FormatNumericalData();
            Console.ReadLine();
        }
        #region  Formatting
        //  Now  make  use  of  some  format  tags.
        static  void  FormatNumericalData()
        {
                /*
                  C  或  c                    货币                        Console.Write("{0:C}",  2.5);          $2.50
                                                                                  Console.Write("{0:C}",  -2.5);        ($2.50)
                  D  或  d                  十进制数                  Console.Write("{0:D5}",  25);            00025
 
                  E  或  e                  科学型                      Console.Write("{0:E}",  250000);    2.500000E+005
                  F  或  f                  固定点                      Console.Write("{0:F2}",  25);          25.00
                                                                                  Console.Write("{0:F0}",  25);          25
                  G  或  g                  常规                          Console.Write("{0:G}",  2.5);          2.5
                  N  或  n                  数字                          Console.Write("{0:N}",  2500000);  2,500,000.00
                  X  或  x                  十六进制                  Console.Write("{0:X}",  250);          FA
                                                                                  Console.Write("{0:X}",  0xffff);    FFFF
                  */
            Console.WriteLine("The  value  99999  in  various  formats:");
            Console.WriteLine("c  format:  {0:c}",  99999);
           
            Console.WriteLine("d9  format:  {0:d9}",  99999);
            //指定小数位数的浮点数表示
            Console.WriteLine("f3  format:  {0:f3}",  99999);
            Console.WriteLine("n  format:  {0:n}",  99999);
           
            //  Notice  that  upper  or  lower  casing  for  hex 
            //  determines  if  letters  are  upper/lowercase.
            //科学计数法表示   
            Console.WriteLine("E  format:  {0:E}",  99999);
            Console.WriteLine("e  format:  {0:e}",  99999);
            //十六进制表示
            Console.WriteLine("X  format:  {0:X}",  99999);
            Console.WriteLine("x  format:  {0:x}",  99999);
        }
        #endregion
        #region  Get  user  data.
        static  void  GetUserData()
        {
            //  Get  name  and  age.
            Console.Write("Please  enter  your  name:  ");
            string  userName  =  Console.ReadLine();
            Console.Write("Please  enter  your  age:  ");
            string  userAge  =  Console.ReadLine();
            //  Change  echo  color,  just  for  fun.
            ConsoleColor  prevColor  =  Console.ForegroundColor;
            Console.ForegroundColor  =  ConsoleColor.Yellow;
            //  Echo  to  the  console.
            Console.WriteLine("Hello  {0}!    You  are  {1}  years  old.",
                    userName,  userAge);
            //  Restore  origional  color.
            Console.ForegroundColor  =  prevColor;
        }
        #endregion
    }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多