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月

相关推荐
新手unity自用笔记12 小时前
项目-坦克大战学习-子弹的移动与销毁
笔记·学习·c#
qinzechen13 小时前
分享几个做题网站------学习网------工具网;
java·c语言·c++·python·c#
yufei-coder17 小时前
C# Windows 窗体开发基础
vscode·microsoft·c#·visual studio
dangoxiba17 小时前
[Unity Demo]从零开始制作空洞骑士Hollow Knight第十三集:制作小骑士的接触地刺复活机制以及完善地图的可交互对象
游戏·unity·visualstudio·c#·游戏引擎
AitTech17 小时前
深入理解C#中的TimeSpan结构体:创建、访问、计算与格式化
开发语言·数据库·c#
hiyo58521 小时前
C#中虚函数和抽象函数的概念
开发语言·c#
开心工作室_kaic1 天前
基于微信小程序的校园失物招领系统的设计与实现(论文+源码)_kaic
c语言·javascript·数据库·vue.js·c#·旅游·actionscript
时光追逐者1 天前
WaterCloud:一套基于.NET 8.0 + LayUI的快速开发框架,完全开源免费!
前端·microsoft·开源·c#·.net·layui·.netcore
friklogff1 天前
【C#生态园】打造现代化跨平台应用:深度解析.NET桌面应用工具
开发语言·c#·.net
hiyo5852 天前
C#的面向对象
开发语言·c#