c# 控制台格式化输出小结

格式化数字

格式字符    说明和关联属性

-------------------------------------------------------------------------------- 

c、C       货币格式。

d、D       十进制格式。

e、E       科学计数(指数)格式。

f、F        固定点格式。

g、G       常规格式。

n、N       数字格式。

r、R        往返格式,确保将已转换成字符串的数字转换回数字时具有与原数字相同的值。

x、X       十六进制格式。

--------------------------------------------------------------------------------

 示例:

double val = Math.PI; Console.WriteLine(val.ToString()); //displays 3.14159265358979
Console.WriteLine(val.ToString("E"));//displays 3.141593E+000
Console.WriteLine(val.ToString("F3"));//displays 3.142
Single val2=0.123F;Console.WriteLine(val2.ToString("p")); //displays 12.30 %
Console.WriteLine(val2.ToString("p1")); //displays 12.3 %
int val3=65535;
Console.WriteLine(val3.ToString(
"x")); //displays ffff
Console.WriteLine(val3.ToString("X")); //displays FFFF
decimal val4 = 323.42313m;
Console.WriteLine(
"{0,9:f2}",val4); // displays 323.42
//{0,9:f2}表示第0个参数,占9位右对齐,小数点精确两位
Console.WriteLine("{0,-9:f2}", val4); // -9表示占9位,左对齐

 默认格式化会在数字和百分号之间放入一个空格。定制方法如下:

其中NumberFormatInfo类是System.Globalization命名空间的一个成员,因此该命名空间必须导入到程序中。

Single val=0.123F;

object myobj=NumberFormatInfo.CurrentInfo.Clone( ) as NumberFormatInfo;

NumberFormatInfo myformat
=myobj as NumberFormatInfo;

myformat.PercentPositivePattern
=1;

Console.WriteLine(val.ToString(
"p",myformat)); //displays 12.30%;

Console.WriteLine(val.ToString(
"p1",myformat)); //displays 12.3%;

 

 格式化具有很大的灵活性。下面的例子演示一个没有意义的货币结构:

double val=1234567.89;

int [] groupsize={2,1,3};

object myobj=NumberFormatInfo.CurrentInfo.Clone( );

NumberFormatInfo mycurrency
=myobj as NumberFormatInfo;

mycurrency.CurrencySymbol
="#"; //符号

mycurrency.CurrencyDecimalSeparator
=":"; //小数点

mycurrency.CurrencyGroupSeparator
="_"; //分隔符

mycurrency.CurrencyGroupSizes
=groupsize;

Console.WriteLine(val.ToString(
"C",mycurrency));

 

// 输出 #1_234_5_67:89 

格式化日期

输出形式取决于用户计算机的文化设置。

using System;

using System.Globalization;

public class MainClass

{

  
public static void Main(string[] args)

  {

DateTime dt
= DateTime.Now;

String[] format
= {"d","D","f","F","g","G","m","r","s","t",
"T","u", "U","y","dddd, MMMM dd yyyy",
"ddd, MMM d \"'\"yy","dddd, MMMM dd","M/yy","dd-MM-yy",};

String date;

for (int i = 0; i < format.Length; i++)

{

date
= dt.ToString(format, DateTimeFormatInfo.InvariantInfo);

Console.WriteLine(String.Concat(format,
" :" , date));

}

}

}

 d :07/11/2004    <=======输出

D :Sunday, 11 July 2004

f :Sunday, 11 July 2004 10:52

F :Sunday, 11 July 2004 10:52:36

g :07/11/2004 10:52

G :07/11/2004 10:52:36

m :July 11

r :Sun, 11 Jul 2004 10:52:36 GMT

s :2004-07-11T10:52:36

t :10:52

T :10:52:36

u :2004-07-11 10:52:36Z

U :Sunday, 11 July 2004 02:52:36

y :2004 July

dddd, MMMM dd yyyy :Sunday, July 11 2004

ddd, MMM d "'"yy :Sun, Jul 11 '04

dddd, MMMM dd :Sunday, July 11

M/yy :7/04

dd-MM-yy :11-07-04

 

 格式化枚举

enum classmen

{
ynn
=1,

yly
=2,

css
=3,

C
++=4
}

 

获取枚举字符串信息如下:

classmen myclassmen=classmen.yly;

Console.WriteLine(myclassmen.ToString( ));
//displays yly

Console.WriteLine(myclassmen.ToString(
"d")); //displays 2

从系统枚举中获取文本人信息如下:

DayOfWeek day=DayOfWeek.Friday;

//displays "Day is Friday"

Console.WriteLine(String.Format(
"Day is {0:G}",day));

格式化字符串"G ”把枚举显示为一个字符串。

posted on 2010-12-05 22:59  I过T  阅读(4654)  评论(0编辑  收藏  举报

导航