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月

相关推荐
qq_454245032 小时前
增强型ECS(Entity-Component-System)框架
架构·c#
njsgcs4 小时前
solidworks导出展开 c# ExportFlatPatternView方法
c#
格林威4 小时前
工业相机图像高速存储(C#版):内存映射文件方法,附Basler相机C#实战代码!
开发语言·人工智能·数码相机·c#·机器视觉·工业相机·堡盟相机
缺点内向4 小时前
C#实战:使用Spire.Doc for .NET 获取并替换Word文档中的字体
c#·自动化·word·.net
荔枝吻4 小时前
【保姆级喂饭教程】Visual Studio 2026 中创建基于 c# 的 WinForms 入门教程
ide·c#·visual studio
bugcome_com4 小时前
【C#进阶】索引器(Indexer)全解析:基础、实战、优化与多场景应用
c#
阿蒙Amon4 小时前
C#常用类库-详解Autofac
开发语言·c#
格林威4 小时前
工业相机图像高速存储(C#版):内存映射文件方法,附堡盟相机C#实战代码!
开发语言·人工智能·数码相机·计算机视觉·c#·工业相机·堡盟相机
格林威4 小时前
工业相机图像高速存储(C#版):先存内存,后批量转存方法,附堡盟 (Baumer) 相机实战代码!
开发语言·人工智能·数码相机·opencv·计算机视觉·c#·halcon
baivfhpwxf20234 小时前
WPF Binding 绑定 超详细详解
c#·wpf