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月

相关推荐
阿蒙Amon16 小时前
C#每日面试题-简述异常处理
开发语言·c#
数据的世界0120 小时前
C#4.0权威指南第10章:继承
c#
小熊熊知识库1 天前
MinIO文件对象存储使用(C#)
c#
Lv11770081 天前
Visual Studio中的二维数组和交错数组
ide·笔记·c#·visual studio
阿蒙Amon1 天前
C#每日面试题-值传递和引用传递的区别
java·面试·c#
yj爆裂鼓手1 天前
unity对象池
unity·c#
Traced back1 天前
Windows窗体应用 + SQL Server 自动清理功能方案:按数量与按日期双模式
数据库·windows·c#·.net
阿蒙Amon1 天前
C#每日面试题-索引器和迭代器的区别
开发语言·windows·c#
1314lay_10071 天前
.Net 7.0 Core 文件导入接口的实现
visualstudio·c#·.net
工业甲酰苯胺1 天前
C#中的多级缓存架构设计与实现深度解析
缓存·c#·wpf