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月

相关推荐
钰fly1 小时前
C#文件与数据操作核心概念手册
c#
阿蒙Amon2 小时前
C#每日面试题-简述C#访问修饰符
windows·microsoft·c#
酩酊仙人4 小时前
ABP+Hangfire实现定时任务
后端·c#·asp.net·hangfire
阿蒙Amon5 小时前
C#每日面试题-属性和特性的区别
java·面试·c#
要记得喝水6 小时前
某公司C#-WPF面试题-来自nowcoder(含答案和解析)--2
c#·wpf
爱敲点代码的小哥9 小时前
类型转换 递归算法 编译错误 装箱和拆箱 知识点
开发语言·c#
时光追逐者10 小时前
一个 WPF 开源、免费的 SVG 图像查看控件
开源·c#·.net·wpf
江沉晚呤时10 小时前
构建智能代理的利器:深入解析 Microsoft Agent Framework
开发语言·c#
武藤一雄11 小时前
C# 中线程安全都有哪些
后端·安全·微软·c#·.net·.netcore·线程
wuguan_11 小时前
C#递推算法
算法·c#·递推算法