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月

相关推荐
颜x小4 小时前
[C#]泛型类与泛型方法
开发语言·c++·c#
caishenzhibiao4 小时前
顺势交易矩阵主图 同花顺期货通指标
java·c语言·c#
TDengine (老段)5 小时前
TDengine Node.js 与 C# 连接器 — Web 服务与 .NET 集成
大数据·数据库·node.js·c#·.net·时序数据库·tdengine
河西石头6 小时前
再谈C#的抽象类和接口:从“适配器”到“毛坯房”的设计哲学
开发语言·c#·接口·依赖注入·抽象类·依赖倒挂
颜x小6 小时前
[C#]——接口与继承
开发语言·c++·c#
颜x小20 小时前
[C#] C++与c#语法对比
开发语言·c++·c#
weixin_4236521320 小时前
C#使用JObject
开发语言·c#
Z59981784121 小时前
c#软件开发学习笔记--Modbus-RTU通讯
笔记·学习·c#
用户298698530141 天前
在 PC 上将 HTML 转换为 PDF 的3种有效方法
人工智能·c#·html
Rotion_深1 天前
C# 设计TCP Server
开发语言·tcp/ip·c#