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

2024-03-21 18:28

本文主要是介绍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.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# System.Console.WriteLine的格式化输出的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

C# 委托中 Invoke/BeginInvoke/EndInvoke和DynamicInvoke 方法的区别和联系

《C#委托中Invoke/BeginInvoke/EndInvoke和DynamicInvoke方法的区别和联系》在C#中,委托(Delegate)提供了多种调用方式,包括Invoke、Begi... 目录前言一、 Invoke方法1. 定义2. 特点3. 示例代码二、 BeginInvoke 和 EndI

C#中的 Dictionary常用操作

《C#中的Dictionary常用操作》C#中的DictionaryTKey,TValue是用于存储键值对集合的泛型类,允许通过键快速检索值,并且具有唯一键、动态大小和无序集合的特性,常用操作包括添... 目录基本概念Dictionary的基本结构Dictionary的主要特性Dictionary的常用操作

Spring AI集成DeepSeek实现流式输出的操作方法

《SpringAI集成DeepSeek实现流式输出的操作方法》本文介绍了如何在SpringBoot中使用Sse(Server-SentEvents)技术实现流式输出,后端使用SpringMVC中的S... 目录一、后端代码二、前端代码三、运行项目小天有话说题外话参考资料前面一篇文章我们实现了《Spring

C# winform操作CSV格式文件

《C#winform操作CSV格式文件》这篇文章主要为大家详细介绍了C#在winform中的表格操作CSV格式文件的相关实例,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下... 目录实例一实例效果实现代码效果展示实例二实例效果完整代码实例一实例效果当在winform界面中点击读取按钮时 将csv中

MySQL 日期时间格式化函数 DATE_FORMAT() 的使用示例详解

《MySQL日期时间格式化函数DATE_FORMAT()的使用示例详解》`DATE_FORMAT()`是MySQL中用于格式化日期时间的函数,本文详细介绍了其语法、格式化字符串的含义以及常见日期... 目录一、DATE_FORMAT()语法二、格式化字符串详解三、常见日期时间格式组合四、业务场景五、总结一、

C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

《C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)》本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepS... 目录前言搭建基础环境1、安装 Ollama2、下载 DeepSeek R1 模型客户端 ChatBo

C# string转unicode字符的实现

《C#string转unicode字符的实现》本文主要介绍了C#string转unicode字符的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录1. 获取字符串中每个字符的 Unicode 值示例代码:输出:2. 将 Unicode 值格式化

Rust格式化输出方式总结

《Rust格式化输出方式总结》Rust提供了强大的格式化输出功能,通过std::fmt模块和相关的宏来实现,主要的输出宏包括println!和format!,它们支持多种格式化占位符,如{}、{:?}... 目录Rust格式化输出方式基本的格式化输出格式化占位符Format 特性总结Rust格式化输出方式

C#中读取XML文件的四种常用方法

《C#中读取XML文件的四种常用方法》Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧... 目录XML简介格式C#读取XML文件方法使用XmlDocument使用XmlTextReader/XmlTextWr

springboot日期格式化全局LocalDateTime详解

《springboot日期格式化全局LocalDateTime详解》文章主要分析了SpringBoot中ObjectMapper对象的序列化和反序列化过程,并具体探讨了日期格式化问题,通过分析Spri... 目录分析ObjectMapper与jsonSerializer结论自定义日期格式(全局)扩展利用配置