C# System.Console.WriteLine的格式化输出

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

using System;

namespace Test {
    class TODO {
        static void Main() {
            System.Console.WriteLine("!{1,4:D3}!", 12, 24);
        }
    }
}
! 024!
{index[,alignment][:formatString]}  

其中,{}表示占位
index指索引占位符,这个肯定都知道;
,alignment按字面意思显然是对齐方式,以,为标记;
:formatString就是对输出格式的限定,以:为标记。

那么上面语句的作用就是

将1号元素放入(默认从0开始),
其宽度为4,
用十进制形式输出3位(及不足位补0)
// 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

相关推荐

  1. Python:优雅格式化XML美化输出格式

    2024-03-24 18:34:02       44 阅读
  2. C# System.Console.WriteLine格式化输出

    2024-03-24 18:34:02       17 阅读
  3. 【C语言】格式化输入/输出

    2024-03-24 18:34:02       20 阅读
  4. C++- 格式化输出

    2024-03-24 18:34:02       39 阅读
  5. python格式化输出

    2024-03-24 18:34:02       20 阅读
  6. Python 格式化输出:精确控制你数据表示

    2024-03-24 18:34:02       31 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-24 18:34:02       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-24 18:34:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-24 18:34:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-24 18:34:02       18 阅读

热门阅读

  1. C#面:简述可空类型

    2024-03-24 18:34:02       17 阅读
  2. 【2024-03-18】顺丰春招笔试两道编程题解

    2024-03-24 18:34:02       19 阅读
  3. 【串口开发】android 智能设备开发 知识笔记

    2024-03-24 18:34:02       21 阅读
  4. 学习笔记 | 微信小程序项目day06

    2024-03-24 18:34:02       22 阅读
  5. mysql基础02

    2024-03-24 18:34:02       15 阅读
  6. Kafka系列之:Exactly-once support

    2024-03-24 18:34:02       17 阅读
  7. 海量数据处理和提高系统的并发能力的一些方案

    2024-03-24 18:34:02       20 阅读
  8. 如何在ubuntu 18.04中升级python 3.6到3.7

    2024-03-24 18:34:02       19 阅读
  9. CCSK-云计算安全基础知识认证

    2024-03-24 18:34:02       18 阅读
  10. OpenCV中如何进行模板匹配?

    2024-03-24 18:34:02       19 阅读
  11. 解释C语言中的函数及其参数传递方式

    2024-03-24 18:34:02       21 阅读
  12. 深入理解PHP+Redis实现分布式锁的相关问题

    2024-03-24 18:34:02       15 阅读
  13. 樊登读书-《终生成长》【视频笔记 +个人思考】

    2024-03-24 18:34:02       16 阅读