格式项都采用如下形式:

{index[,alignment][:formatString]}

其中"index"指索引占位符,这个肯定都知道;

",alignment"按字面意思显然是对齐方式,以","为标记;

":formatString"就是对输出格式的限定,以":"为标记。

alignment:可选,是一个带符号的整数,指示首选的格式化字段宽度。如果“对齐”值小于格式化字符串的长度,“对齐”会被忽略,并且使用格式化字符串的长度作为字段宽度。如果“对齐”为正数,字段的格式化数据为右对齐;如果“对齐”为负数,字段的格式化数据为左对齐。如果需要填充,则使用空白。如果指定“对齐”,就需要使用逗号。

formatString:由标准或自定义格式说明符组成.

下表是从网上得来:

字符

说明

示例

输出

C

货币

string.Format("{0:C3}", 2)

$2.000

D

十进制

string.Format("{0:D3}", 2)

002

E

科学计数法

1.20E+001

1.20E+001

G

常规

string.Format("{0:G}", 2)

2

N

用分号隔开的数字

string.Format("{0:N}", 250000)

250,000.00

X

十六进制

string.Format("{0:X000}", 12)

C

string.Format("{0:000.000}", 12.2)

012.200

Specifier

Type

Format

Output
(Passed
Double 1.42)

Output
(Passed
Int -12400)

c

Currency

{0:c}

$1.42

-$12,400

d

Decimal (Whole number)

{0:d}

System.
FormatException

-12400

e

Scientific

{0:e}

1.420000e+000

-1.240000e+004

f

Fixed point

{0:f}

1.42

-12400.00

g

General

{0:g}

1.42

-12400

n

Number with commas for thousands

{0:n}

1.42

-12,400

r

Round trippable

{0:r}

1.42

System.
FormatException

x

Hexadecimal

{0:x4}

System.
FormatException

cf90

Specifier

Type

Example (Passed System.DateTime.Now)

d

Short date

10/12/2002

D

Long date

December 10, 2002

t

Short time

10:11 PM

T

Long time

10:11:29 PM

f

Full date & time

December 10, 2002 10:11 PM

F

Full date & time (long)

December 10, 2002 10:11:29 PM

g

Default date & time

10/12/2002 10:11 PM

G

Default date & time (long)

10/12/2002 10:11:29 PM

M

Month day pattern

December 10

r

RFC1123 date string

Tue, 10 Dec 2002 22:11:29 GMT

s

Sortable date string

2002-12-10T22:11:29

u

Universal sortable, local time

2002-12-10 22:13:50Z

U

Universal sortable, GMT

December 11, 2002 3:13:50 AM

Y

Year month pattern

December, 2002

Specifier

Type

Example

Example Output

dd

Day

{0:dd}

10

ddd

Day name

{0:ddd}

Tue

dddd

Full day name

{0:dddd}

Tuesday

f, ff, ...

Second fractions

{0:fff}

932

gg, ...

Era

{0:gg}

A.D.

hh

2 digit hour

{0:hh}

10

HH

2 digit hour, 24hr format

{0:HH}

22

mm

Minute 00-59

{0:mm}

38

MM

Month 01-12

{0:MM}

12

MMM

Month abbreviation

{0:MMM}

Dec

MMMM

Full month name

{0:MMMM}

December

ss

Seconds 00-59

{0:ss}

46

tt

AM or PM

{0:tt}

PM

yy

Year, 2 digits

{0:yy}

02

yyyy

Year

{0:yyyy}

2002

zz

Timezone offset, 2 digits

{0:zz}

-05

zzz

Full timezone offset

{0:zzz}

-05:00

:

Separator

{0:hh:mm:ss}

10:43:20

/

Separator

{0:dd/MM/yyyy}

10/12/2002

示例:

//Console.WriteLine 中各种数据格式的输出

Console.WriteLine("{0,8 :C}",2); //$2.00

Console.WriteLine("{0,8 :C3}",2); //$2.000

Console.WriteLine("{0:D3}",2); //002

Console.WriteLine("{0:E}",2); //2.000000E+000

Console.WriteLine("{0:G}",2); //2

Console.WriteLine("{0:N}",2500000.00); //2,500,00.00

Console.WriteLine("{0:x4}",12); // 000c

Console.WriteLine("{0,2 :x}",12); // c

Console.WriteLine("{0:000.000}",12.23); //012.230

Console.WriteLine("{0:r}",15.62); //15.62

Console.WriteLine("{0:d}", System.DateTime.Now); // 2012-3-27

Console.WriteLine("{0:D}", System.DateTime.Now); // 2012年3月27日

Console.WriteLine("{0:t}", System.DateTime.Now); //11:43

Console.WriteLine("{0:T}", System.DateTime.Now); //11:43:34

Console.WriteLine("{0:f}", System.DateTime.Now); //2012年3月27日 11:43

Console.WriteLine("{0:F}", System.DateTime.Now); //2012年3月27日 11:43:34

Console.WriteLine("{0:g}", System.DateTime.Now); // 2012-3-2711:43

Console.WriteLine("{0:G}", System.DateTime.Now); // 2012-3-2711:43:34

Console.WriteLine("{0:M}", System.DateTime.Now); // 3月27日

Console.WriteLine("{0:r}", System.DateTime.Now);// Tue, 27 Mar 201211:43:34 GMT

Console.WriteLine("{0:s}", System.DateTime.Now); // 2012-03-27T11:43:34

Console.WriteLine("{0:u}", System.DateTime.Now); // 2012-03-2711:43:34Z

Console.WriteLine("{0:U}", System.DateTime.Now); //2012年3月27日 3:43:34

Console.WriteLine("{0:Y}", System.DateTime.Now); //2012年3月

Console.WriteLine("{0:dd}", System.DateTime.Now); //27

Console.WriteLine("{0:ddd}", System.DateTime.Now); // 二

Console.WriteLine("{0:dddd}", System.DateTime.Now);// 星期二

Console.WriteLine("{0:f}", System.DateTime.Now); //2012年3月27日 11:46

Console.WriteLine("{0:ff}", System.DateTime.Now); //18

Console.WriteLine("{0:fff}", System.DateTime.Now); //187

Console.WriteLine("{0:ffff}", System.DateTime.Now); // 1875

Console.WriteLine("{0:fffff}", System.DateTime.Now); // 18750

Console.WriteLine("{0:gg}", System.DateTime.Now); // 公元

Console.WriteLine("{0:ggg}", System.DateTime.Now); // 公元

Console.WriteLine("{0:gggg}", System.DateTime.Now); // 公元

Console.WriteLine("{0:ggggg}", System.DateTime.Now); // 公元

Console.WriteLine("{0:gggggg}", System.DateTime.Now); // 公元

Console.WriteLine("{0:hh}", System.DateTime.Now); //11

Console.WriteLine("{0:HH}", System.DateTime.Now); //11

Console.WriteLine("{0:mm}", System.DateTime.Now); //50

Console.WriteLine("{0:MM}", System.DateTime.Now); //03

Console.WriteLine("{0:MMM}", System.DateTime.Now); // 三月

Console.WriteLine("{0:MMMM}", System.DateTime.Now);// 三月

Console.WriteLine("{0:ss}", System.DateTime.Now); //43

Console.WriteLine("{0:tt}", System.DateTime.Now); // 上午

Console.WriteLine("{0:yy}", System.DateTime.Now); //12

Console.WriteLine("{0:yyyy}", System.DateTime.Now); // 2012

Console.WriteLine("{0:zz}", System.DateTime.Now); //+08

Console.WriteLine("{0:zzz}", System.DateTime.Now); //+08:00

Console.WriteLine("{0:hh:mm:ss}", System.DateTime.Now); //11:43:34

Console.WriteLine("{0:dd/MM/yyyy}", System.DateTime.Now);// 27-03-2012

C#中Console.WriteLine()函数输出格式详解相关推荐

  1. python中subplot是什么意思_python matplotlib中的subplot函数使用详解

    python里面的matplotlib.pylot是大家比较常用的,功能也还不错的一个包.基本框架比较简单,但是做一个功能完善且比较好看整洁的图,免不了要网上查找一些函数.于是,为了节省时间,可以一劳 ...

  2. python中mat函数_python matplotlib中的subplot函数使用详解

    python里面的matplotlib.pylot是大家比较常用的,功能也还不错的一个包.基本框架比较简单,但是做一个功能完善且比较好看整洁的图,免不了要网上查找一些函数.于是,为了节省时间,可以一劳 ...

  3. python getopt_python 5种 statsPython中的getopt函数使用详解

    函数原型: getopt.getopt(args, shortopts, longopts=[]) 参数解释: args:args为需要解析的参数列表.一般使用sys.argv[1:],这样可以过滤掉 ...

  4. python explode_pandas dataframe 中的explode函数用法详解

    在使用 pandas 进行数据分析的过程中,我们常常会遇到将一行数据展开成多行的需求,多么希望能有一个类似于 hive sql 中的 explode 函数. 这个函数如下: Code # !/usr/ ...

  5. pythonsubplot_python matplotlib中的subplot函数使用详解

    python里面的matplotlib.pylot是大家比较常用的,功能也还不错的一个包.基本框架比较简单,但是做一个功能完善且比较好看整洁的图,免不了要网上查找一些函数.于是,为了节省时间,可以一劳 ...

  6. python中的iloc函数_详解pandas中利用DataFrame对象的.loc[]、.iloc[]方法抽取数据

    pandas的DataFrame对象,本质上是二维矩阵,跟常规二维矩阵的差别在于前者额外指定了每一行和每一列的名称.这样内部数据抽取既可以用"行列名称(对应.loc[]方法)",也 ...

  7. bootstraptable 加载完成回调函数_牛皮了!头一次见有大佬把「JavaScript中的回调函数」详解得如此清晰明了...

    前言 callback,大家都知道是回调函数的意思.但是你对这个概念应该是模模糊糊.比如Ajax,你只知道去调用返回函数,如果对callback没有理解清楚,估计你在学习Node.js后会崩溃,因为c ...

  8. java中static修饰函数_详解java中static关键词的作用

    在java中,static是一个修饰符,用于修饰类的成员方法.类的成员变量,另外可以编写static代码块来优化程序性能:被static关键字修饰的方法或者变量不需要依赖于对象来进行访问,只要类被加载 ...

  9. JavaScript中立即执行函数实例详解 转载 作者:李牧羊

    javascript和其他编程语言相比比较随意,所以javascript代码中充满各种奇葩的写法,有时雾里看花,当然,能理解各型各色的写法也是对javascript语言特性更进一步的深入理解.这篇文章 ...

最新文章

  1. 币圈“水逆”,暴跌与高危漏洞
  2. 不要和别人说你一定要干什么
  3. 转帖--一封爸妈给女儿写的信
  4. 从案例角度解析建模平台动态规则引擎
  5. Andrew Ng深度学习课程笔记
  6. Delphi获得计算机系统配置,用Delphi获取本地计算机名称和IP地址
  7. 库存产品日期查询功能sql优化
  8. 内涝预测过程的噪音_第七章噪声解析
  9. 【C/C++】最大公约数和最小公倍数(辗转相除、更相减损、stein)
  10. 用U3D寻找看电视的感觉!!
  11. DEM数据(12米)精度获取方法
  12. tp5.0 api 接口设计语言包切换功能
  13. 富士施乐m115b怎么连接电脑_富士施乐m115b驱动下载
  14. sap 流程图 退货销售订单_ERP系统:退货流程的解决方案
  15. pyton构建一个计算列表中位数的函数
  16. 【转载】:马家寨:揭开陈圆圆携吴三桂后裔隐藏与繁衍真相
  17. wps在Linux上使用命令打开文档的方法
  18. movie起居类分类词汇
  19. 光纤熔接机的光纤对准方式
  20. java中文拼音转换(maven)

热门文章

  1. hive like在join中的应用二(进阶)
  2. 会计凭证合并OBCY
  3. 自定义百度地图style样式
  4. 苹果因键盘不好使赔3.4亿,SpaceX接单韦布后继者,META起诉Meta,今日更多新鲜事在此...
  5. 第二十七章 数据劫持
  6. 【阿里淘宝天池Baseline服装搭配比赛】尝试一
  7. WSL2中设置DNS后重启终端又失效的解决方法
  8. good study 网站(ebook下载)
  9. 触摸板的使用——windows系统笔记本
  10. 实时排名算法简单分析