C#字符串格式化

数值规范

也可写成int money = 368; money .ToString("C");

string.Format("金额:{0:C}", 368); => ¥368.00

string.Format("科学计数法:{0:C}", 12000.1); => 1.200001E+005

string.Format("分割数字:,{0:E0}", 123456789); => 123,456,789

string.Format("π取两位小数:{0:F2}", Math.PI); => 3.14

string.Format("16进制:{0:X4}", 33); => 0021

string.Format("百分比:{0:P0}", 0.99); => 99%

日期格式化

DateTime strDate = DateTime.Now;

也可写成strDate.ToString("Y");

string.Format("短日期格式:{0:d}", strDate); =>短日期格式:2024/4/29

string.Format("长日期格式:{0:D}", strDate); =>长日期格式:2024年4月29日

string.Format("完整日期格式:{0:f}", strDate); =>完整日期格式:2024年4月29日 16:26

string.Format("短时间格式:{0:t}", strDate); =>短时间格式:16:26

string.Format("长时间格式:{0:T}", strDate); =>长时间格式:16:26:54

string.Format("月日格式:{0:M}", strDate); =>月日格式:4月29日

string.Format("年月格式:{0:y}", strDate); =>年月格式:2024年4月

相关推荐
阿蒙Amon3 小时前
C#每日面试题-Thread.Sleep和Task.Delay的区别
java·数据库·c#
cfqq19894 小时前
Settings,变量保存
开发语言·c#
云草桑5 小时前
.net AI开发04 第八章 引入RAG知识库与文档管理核心能力及事件总线
数据库·人工智能·microsoft·c#·asp.net·.net·rag
曹牧7 小时前
C#:窗体构造函数无法引用窗体控件
开发语言·c#
iAkuya7 小时前
(leetcode)力扣100 54实现Trie树
算法·leetcode·c#
xb11327 小时前
C#使用Cancellation来取消异步任务
开发语言·c#
m0_748229997 小时前
C与C#:编程语言的核心差异解析
c语言·开发语言·c#
m0_748229998 小时前
Laravel7.x核心特性全解析
c语言·数据库·c#
阿蒙Amon8 小时前
C#每日面试题-Task和Thread的区别
java·面试·c#
ytttr8739 小时前
C#实现海康威视智能车牌识别
开发语言·c#