Console.WriteLine()方法
首先先用一个例子:
using System;//导入System命名空间
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApp1//声明命名空间
{class Program{static void Main(string[] args){Console.WriteLine("{0},{1},{2},{1}", "a", "b", "c");Console.ReadLine();}}
}
输出:
a,b,c,d;
其中 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
Console.WriteLine()方法相关推荐
- .Net5下Console.WriteLine()方法无输出
项目场景: 使用VS2019社区版,学习.Net5下的WPF程序开发 问题描述: 在学习过程中: 发现在WPF程序中,Console.WriteLine()函数无法输出调试信息 private voi ...
- C#里面Console.Write与Console.WriteLine有什么区别????
Write()和WriteLine()都是System.Console提供的方法,两着主要用来将输出流由指定的输出装置(默认为屏幕)显示出来.两着间的差异在Console.WriteLine()方法是 ...
- writeline是什么意思_c语言console.WriteLine什么意思?
console.WriteLine是指输出到屏幕,常用于控制台程序中,输出内容为是一行.Console.WriteLine()方法是将要输出的字符串与换行控制字符一起输出,当次语句执行完毕时,光标会移 ...
- C#程序演示Console.Write()和Console.WriteLine()的示例
Console.Write() and Console.WriteLine() methods are used to print the text (values) on the Console. ...
- c语言开发console,c语言console.WriteLine什么意思?
摘要:console.WriteLine是指输出到屏幕,常用于控制台程序中,输出内容为是一行.Console.WriteLine()方法是将要输出的字符串与换行控制字符一起输出,当次语句执行完毕时,光 ...
- 关于 C#使用Console.WriteLine调试没有命令行输出 的解决方法
若该文为原创文章,转载请注明原文出处 本文章博客地址:https://blog.csdn.net/qq21497936/article/details/115211788 长期持续带来更多项目与技术分 ...
- Console.WriteLine在以Windows Application方式下编译会产生性能问题
新接触C#+.net 3.5,用C#重写了一个以前用Qt写的串口操作程序,并以Class Library方式编译,测试程序是以Console Application方式进行编译的C#程序,把发送和接收 ...
- C# Console.ReadLine()方法的使用 以及利用其返回值null终止输入
官方解释 ReadLine方法以同步方式执行. 即,被阻止,直至读取行或按下 Ctrl + Z 键盘组合. In属性返回TextReader对象,它表示标准输入的流并具有这两个同步 TextReade ...
- Visual Studio 调试 .net mvc 项目 Console.WriteLine 无法输出到控制台 解决方案
Intro env Visual Studio 2019 Community .net framework 4.7.2 解决 使用了其他语句,将想要查看的信息打印在Output窗口. Operatio ...
最新文章
- 超越RetinaFace,腾讯优图 ASFD 已在 WIDER FACE 霸榜半年!
- ​Python 3 新特性:类型注解——类似注释吧,反正解释器又不做校验
- 互联网运营遇到瓶颈?这套数据运营体系,高手和小白都必看
- Spring中父子容器的实现实例
- 【nginx】nginx 动静分离
- Linux常见目录含义及功能
- cisco 的端口聚合
- ROC曲线及AUC值
- git revert 之后怎么撤销_Git撤销回滚操作(git reset 和 get revert)
- 如何不获取root权限使用第三方主题:MIUI篇
- 信安小组 第三周 总结
- Java 细胞分裂问题
- 先秦经典智慧名言故事丛书(全16册) 内容简介
- 我和后端因为接口吵起来啦,还列了 5 锅罪
- python读取excel日期小数_RPA-使用Python读取Excel日期结果为数字时的转换处理方法...
- 云计算机手机apple,云电脑IOS版怎么在苹果IOS手机上使用教程
- 精品软件 推荐 ABBYY FineReader 世界排名第一的 OCR 文字识别工具
- 纯前端大数据处理技术:葡萄城纯前端开发工具应用实践
- web前端从入门到放弃
- 各种ADSL MODEM端口影射
热门文章
- Centos 7.5 如何安装VMware Tools工具
- 算法(1) 冒泡排序
- 《QDebug 2022年5月》
- html/css/Bootstrap/Font Awesome
- cpu二级缓存和一级缓存详解及区别(图解)
- 163、交换机的三种vlan划分方法详解,划分vlan不再难
- Bluetooth篇 开发实例之十 官网的Bluetooth Chat sample app.
- Github每日精选:超6k星的开源神器,一键还原百年老电影、黑白旧照片本色
- WPF控件置顶及置底功能
- 【在线研讨】《敏捷开发用户故事分类与组织结构(三期-4)》