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

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

MySQL 删除数据详解(最新整理)

《MySQL删除数据详解(最新整理)》:本文主要介绍MySQL删除数据的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、前言二、mysql 中的三种删除方式1.DELETE语句✅ 基本语法: 示例:2.TRUNCATE语句✅ 基本语

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客