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月

相关推荐
etp_7 分钟前
C#异步编程
c#
Xin_ye1008627 分钟前
C# 零基础到精通教程 - WPF 深度专题:自定义布局与性能优化
开发语言·c#·wpf
焚 城31 分钟前
Winform双语实现
c#·winform
雪豹阿伟40 分钟前
16.C# —— 委托,委托实例,多播委托,内置委托,泛型委托
c#·上位机
小满Autumn1 小时前
WPF 依赖属性速查手册
笔记·c#·wpf·上位机·mvvm
JaydenAI1 小时前
[MAF预定义ChatClient中间件-09]MessageInjectingChatClient-赋予工具消息注入的能力
ai·c#·agent·maf·ichatclient
Xin_ye100861 小时前
C# 零基础到精通教程 - WPF 深度专题:3D 图形与视觉增强
开发语言·c#·wpf
Chris _data3 小时前
并发单词频率统计器 - 从零到完整实现(C# 实战)
开发语言·c#
iCxhust13 小时前
C# 命令行指令 查看二进制文件
开发语言·单片机·嵌入式硬件·c#·proteus·微机原理·8088单板机