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

2024-03-21 08:20

本文主要是介绍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
(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.00Console.WriteLine("{0, 8 :C3}", 2);    // $2.000Console.WriteLine("{0 :D3}", 2);       // 002Console.WriteLine("{0 :E}", 2);        // 2.000000E+000Console.WriteLine("{0 :G}", 2);        // 2Console.WriteLine("{0 :N}", 2500000.00);    // 2,500,00.00Console.WriteLine("{0 :x4}", 12);      // 000cConsole.WriteLine("{0, 2 :x}", 12);    //  cConsole.WriteLine("{0 :000.000}", 12.23);   // 012.230Console.WriteLine("{0 :r}", 15.62);    // 15.62Console.WriteLine("{0 :d}", System.DateTime.Now);    // 2012-3-27Console.WriteLine("{0 :D}", System.DateTime.Now);    // 2012年3月27日Console.WriteLine("{0 :t}", System.DateTime.Now);    // 11:43Console.WriteLine("{0 :T}", System.DateTime.Now);    // 11:43:34Console.WriteLine("{0 :f}", System.DateTime.Now);    // 2012年3月27日 11:43Console.WriteLine("{0 :F}", System.DateTime.Now);    // 2012年3月27日 11:43:34Console.WriteLine("{0 :g}", System.DateTime.Now);    // 2012-3-27 11:43Console.WriteLine("{0 :G}", System.DateTime.Now);    // 2012-3-27 11:43:34Console.WriteLine("{0 :M}", System.DateTime.Now);    // 3月27日Console.WriteLine("{0 :r}", System.DateTime.Now);// Tue, 27 Mar 2012 11:43:34 GMTConsole.WriteLine("{0 :s}", System.DateTime.Now);    // 2012-03-27T11:43:34Console.WriteLine("{0 :u}", System.DateTime.Now);    // 2012-03-27 11:43:34ZConsole.WriteLine("{0 :U}", System.DateTime.Now);    // 2012年3月27日 3:43:34Console.WriteLine("{0 :Y}", System.DateTime.Now);    // 2012年3月Console.WriteLine("{0 :dd}", System.DateTime.Now);   // 27Console.WriteLine("{0 :ddd}", System.DateTime.Now);  // 二Console.WriteLine("{0 :dddd}", System.DateTime.Now); // 星期二Console.WriteLine("{0 :f}", System.DateTime.Now);    // 2012年3月27日 11:46Console.WriteLine("{0 :ff}", System.DateTime.Now);   // 18Console.WriteLine("{0 :fff}", System.DateTime.Now);  // 187Console.WriteLine("{0 :ffff}", System.DateTime.Now); // 1875Console.WriteLine("{0 :fffff}", System.DateTime.Now); // 18750Console.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);   // 11Console.WriteLine("{0 :HH}", System.DateTime.Now);   // 11Console.WriteLine("{0 :mm}", System.DateTime.Now);   // 50Console.WriteLine("{0 :MM}", System.DateTime.Now);   // 03Console.WriteLine("{0 :MMM}", System.DateTime.Now);  // 三月Console.WriteLine("{0 :MMMM}", System.DateTime.Now); // 三月Console.WriteLine("{0 :ss}", System.DateTime.Now);   // 43Console.WriteLine("{0 :tt}", System.DateTime.Now);   // 上午Console.WriteLine("{0 :yy}", System.DateTime.Now);   // 12Console.WriteLine("{0 :yyyy}", System.DateTime.Now); // 2012Console.WriteLine("{0 :zz}", System.DateTime.Now);   // +08Console.WriteLine("{0 :zzz}", System.DateTime.Now);  // +08:00Console.WriteLine("{0 :hh:mm:ss}", System.DateTime.Now);  // 11:43:34Console.WriteLine("{0 :dd/MM/yyyy}", System.DateTime.Now); // 27-03-2012

这篇关于C#中Console.WriteLine()函数输出格式详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/832198

相关文章

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

Linux系统性能检测命令详解

《Linux系统性能检测命令详解》本文介绍了Linux系统常用的监控命令(如top、vmstat、iostat、htop等)及其参数功能,涵盖进程状态、内存使用、磁盘I/O、系统负载等多维度资源监控,... 目录toppsuptimevmstatIOStatiotopslabtophtopdstatnmon

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

Java中的数组与集合基本用法详解

《Java中的数组与集合基本用法详解》本文介绍了Java数组和集合框架的基础知识,数组部分涵盖了一维、二维及多维数组的声明、初始化、访问与遍历方法,以及Arrays类的常用操作,对Java数组与集合相... 目录一、Java数组基础1.1 数组结构概述1.2 一维数组1.2.1 声明与初始化1.2.2 访问

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

一文详解SpringBoot中控制器的动态注册与卸载

《一文详解SpringBoot中控制器的动态注册与卸载》在项目开发中,通过动态注册和卸载控制器功能,可以根据业务场景和项目需要实现功能的动态增加、删除,提高系统的灵活性和可扩展性,下面我们就来看看Sp... 目录项目结构1. 创建 Spring Boot 启动类2. 创建一个测试控制器3. 创建动态控制器注

C#读写文本文件的多种方式详解

《C#读写文本文件的多种方式详解》这篇文章主要为大家详细介绍了C#中各种常用的文件读写方式,包括文本文件,二进制文件、CSV文件、JSON文件等,有需要的小伙伴可以参考一下... 目录一、文本文件读写1. 使用 File 类的静态方法2. 使用 StreamReader 和 StreamWriter二、二进

在Linux中改变echo输出颜色的实现方法

《在Linux中改变echo输出颜色的实现方法》在Linux系统的命令行环境下,为了使输出信息更加清晰、突出,便于用户快速识别和区分不同类型的信息,常常需要改变echo命令的输出颜色,所以本文给大家介... 目python录在linux中改变echo输出颜色的方法技术背景实现步骤使用ANSI转义码使用tpu