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月

相关推荐
Never_Satisfied13 小时前
在c#中,抛出异常,并指定其message的值
java·javascript·c#
不想看见40415 小时前
Word Ladder Ⅱ -- 广度优先搜索--力扣101算法题解笔记
开发语言·c#
全栈小516 小时前
【C#】.net 6.0和8.0有什么区别,以及8.0增加了那些功能,这些功能有那些作用
开发语言·c#·.net
qq_2979080116 小时前
C#印刷线路板ERP进销存报价财务库存贸易生产企业管理系统软件
sqlserver·开源·c#·.net·开源软件
bugcome_com16 小时前
C# 循环语句详解:理解常见循环类型与控制语句
c#
JQLvopkk18 小时前
能用C#开发AI
开发语言·人工智能·c#
游乐码1 天前
c#类和对象
开发语言·c#
沃码1 天前
【Lively Wallpaper 】插件开发:LivelyProperties.json 全流程实战教程
c#·json·livelywallpaper·视频壁纸
github.com/starRTC1 天前
Claude Code中英文系列教程34:再谈Skills
开发语言·c#·ai编程
bugcome_com2 天前
C# 判断语句详解(if、switch、三目运算符、Null 条件运算符)
c#