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月

相关推荐
bugcome_com7 小时前
bugcome —— 个人独立开发 6 年商业计划书
c#
bugcome_com12 小时前
C# 中的封装与访问修饰符
c#
游乐码13 小时前
c#成员属性
开发语言·c#
Never_Satisfied14 小时前
在c#中,如何删除字符串中的第x个字符
开发语言·c#
Never_Satisfied15 小时前
在c#中,缩放jpg文件的尺寸
算法·c#
Never_Satisfied15 小时前
在c#中,控件的事件执行耗时操作导致窗体无法及时处理绘制、鼠标点击
开发语言·c#
zls36536515 小时前
C# WPF canvas中绘制缺陷分布map并实现缩放
开发语言·c#·wpf
Never_Satisfied16 小时前
在c#中,如何在字符串的第x个字符位置插入字符
java·开发语言·c#
bugcome_com1 天前
C# 常量详解:从基础语法到实际应用
c#
qq_150841991 天前
3天基于VS2026的C#编程入门及动态调用CH341DLLA64读写I2C从机
开发语言·c#