C#中Console.WriteLine()函数输出格式详解
格式项都采用如下形式:
{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 |
Output |
c |
Currency |
{0:c} |
$1.42 |
-$12,400 |
d |
Decimal (Whole number) |
{0:d} |
System. |
-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. |
x |
Hexadecimal |
{0:x4} |
System. |
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-27 11:43
Console.WriteLine("{0 :G}", System.DateTime.Now); // 2012-3-27 11:43:34
Console.WriteLine("{0 :M}", System.DateTime.Now); // 3月27日
Console.WriteLine("{0 :r}", System.DateTime.Now);// Tue, 27 Mar 2012 11:43:34 GMT
Console.WriteLine("{0 :s}", System.DateTime.Now); // 2012-03-27T11:43:34
Console.WriteLine("{0 :u}", System.DateTime.Now); // 2012-03-27 11: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()函数输出格式详解的更多相关文章
- 7--OC中NSLog函数输出格式详解
OC中NSLog函数输出格式详解 • %@ 对象 • %d, %i 整数 • %u 无符整形 • %f 浮点/双字 • %x, %X 二进制整数 • %o 八进制整数 • %zu size_t • % ...
- OC中NSLog函数输出格式详解
OC中NSLog函数输出格式详解 %@ 对象 • %d, %i 整数 • %u 无符整形 • %f 浮点/双字 • %x, %X 二进制整数 • %o 八进制整数 • %zu size_t • %p ...
- delphi中Application.MessageBox函数用法详解
delphi中Application.MessageBox函数用法详解 Application.MessageBox是TApplication的成员函数,声明如下:functionTApplicati ...
- php中的PDO函数库详解
PHP中的PDO函数库详解 PDO是一个“数据库访问抽象层”,作用是统一各种数据库的访问接口,与mysql和mysqli的函数库相比,PDO让跨数据库的使用更具有亲和力:与ADODB和MDB2相比,P ...
- GCD中的dispatch_group函数的详解
<一>引入dispatch_group函数的目的 在追加到dispatch_Queue中的多个处理全部结束后想要执行结束的处理,这种需求经常会在我们的程序中出现 (第一种情况)只使用一个S ...
- python中os模块函数方法详解最全最新
os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 import os print(os.getcwd()) os.chdir("dirname") 改 ...
- PHP中的 $_SERVER 函数说明详解
用php在开发软件的时候,我们经常用到 $_SERVER[]这个函数,今天就来讲下这个数组的值,方便以后使用: A: $_SERVER['ARGC'] #包含传递给程序的 命令行参数的个数(如果运行在 ...
- MySQL中的RAND()函数使用详解(order by rand() 随机查询取前几条记录)
MySQL RAND()函数调用可以在0和1之间产生一个随机数: mysql> SELECT RAND( ), RAND( ), RAND( ); +------------------+--- ...
- MySQL中的RAND()函数使用详解
转自:https://www.jb51.net/article/66697.htm MySQL RAND()函数调用可以在0和1之间产生一个随机数: ? 1 2 3 4 5 6 7 mysql> ...
随机推荐
- PARTITION BY 和 group by
sum() over (PARTITION BY ...) 是一个分析函数. 他执行的效果跟普通的sum ...group by ...不一样,它计算组中表达式的累 ...
- apache-tomcat-7.0.70无法进入Manager管理App项目
在tomcat文件夹找到conf文件夹中的tomcat-user.xml文件,用记事本打开,在最下面可以看到tomcat默认把用户注释掉了,也就是说打开tomcat主页是进不去管理页面的.方法如下:找 ...
- LightOJ 1030 Discovering Gold(期望)
Description You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell o ...
- 2016中国大学生程序设计竞赛 - 网络选拔赛 1004 Danganronpa
Problem Description Chisa Yukizome works as a teacher in the school. She prepares many gifts, which ...
- 转:WebDriver进行屏幕截图
例: 打开百度首页 ,进行截图 01 packagecom.example.tests; 02 importjava.io.File; 03 importorg.apache.commons.io ...
- 关于this指针理解
1. this指针的用处: 一个对象的this指针并不是对象本身的一部分,不会影响sizeof(对象)的结果.this作用域是在类内部,当在类的非静态成员函数中访问类的非静态成员的时候,编译器会自动将 ...
- linux shell: 取得某个目录下的文件名列表
取得某个目录下文件名的列表(没有子目录) ls -l dir/ | awk '{print "dir\\" $9}' #其中$9是ls -l 的第9个字段-文件名
- cap
http://blog.javachen.com/2014/05/30/note-about-brewers-cap-theorem.html
- 转 oracle 开发 第03章 sqlplus
目录 1.查看表结构 desc 2.编辑SQL语句 append.list.change.run 3.保存.检索并运行文件 save.get.start.edit.spool 4.格式化列 colum ...
- 实例:SSH结合Easyui实现Datagrid的批量删除功能
在我先前的基础上面添加批量删除功能.实现的效果如下 删除成功 通常情况下删除不应该真正删除,而是应该有一个标志flag,但flag=true表示状态可见,但flag=false表示状态不可见,为删除状 ...