C# String.Format大全
C# String.Format大全
| ? | ? | ? |
|
十进制的数字 |
? | ? |
|
string.Format("{0:D3}",23) |
023 |
格式化十进制的数字 |
|
string.Format("{0:D2}",1223) |
1223 |
精度说明符指示结果字符串中所需的最少数字个数 |
|
12345.ToString("n"); |
//生成 12,345.00 |
千位分隔,两个小数 |
|
string.Format("{0:N}", 14200) |
14,200.00 |
默认为小数点后面两位 |
|
string.Format("{0:N3}", 14200.2458) |
14,200.246 |
自动四舍五入 |
|
12345.ToString("e"); |
//生成 1.234500e+004 |
科学计数法 |
|
12345.ToString("f4"); |
//生成 12345.0000 |
保留小数位 |
|
12345.ToString(“f”) |
? |
定点 |
|
12345.ToString("x"); |
//生成 3039 (16进制) |
HEX 大小写 |
|
常规”G”和”g” |
? | ? |
|
12345.ToString("G"); 12345.ToString("g"); |
? |
常规 |
| ? | ? | ? |
|
自定义数字格式 |
? | ? |
|
String.Format("{0:00.0000}",1500.42) |
1500.4200 |
零占位符 |
|
String.Format("{0:(#).##}",1500.42) |
(1500).42 |
数字占位符 |
|
String.Format("{0:0.0}",1500.42) |
1500.4 |
小数点 |
|
String.Format("{0:0,0}",1500.42) |
1,500 |
? |
|
String.Format("{0:0,.}",1500.42) |
2 |
? |
|
String.Format("{0:0%}",1500.42) |
150042% |
百分数 |
|
String.Format("{0:00e+0}",1500.42) |
15e+2 |
指数占位符 |
| ? | ? | ? |
|
货币 |
? | ? |
|
12345.ToString("C"); |
//生成 ¥12,345.00 |
货币符,千位分隔,两个小数 |
|
string.Format("{0:C}",0.2) |
¥0.20 |
英文操作系统结果:$0.20 |
|
string.Format("{0:C1}",23.15) |
¥23.2 |
(截取会自动四舍五入) |
|
string.Format("市场价:{0:C},优惠价{1:C}",23.15,19.82) |
? |
格式化多个Object实例 |
|
String.Format("{0:¥#,##0.00;(¥#,##0.00);零}", 98005.30) |
¥98,005.30 |
金额,千位分隔符,两个小数 |
|
String.Format("{0:¥#,##0.00;(¥#,##0.00);零}", -98005.30) |
(¥98,005.30) |
金额,千位分隔符,两个小数 |
|
String.Format("{0:¥#,##0.00;(¥#,##0.00);零}", 0) |
零 |
金额,千位分隔符,两个小数 |
| ? | ? | ? |
|
百分比 |
? | ? |
|
12345.ToString("p"); |
//生成 1,234,500.00% |
百分数,千位分隔,两个小数 |
|
string.Format("{0:P}", 0.24583) |
24.58% |
默认保留百分的两位小数 |
|
string.Format("{0:P1}", 0.24583) |
24.6% |
自动四舍五入 |
| ? | ? | ? |
|
往返过程 |
||
|
Double.ToString(“R”) Double.ToString(“r”) Single.ToString(“R”) Single.ToString(“r”) |
? |
往返过程,适用于Double和Single |
| ? | ? | ? |
|
零占位符和数字占位符 |
||
|
string.Format("{0:0000.00}", 12394.039) |
12394.04 |
? |
|
string.Format("{0:0000.00}", 194.039) |
0194.04 |
? |
|
string.Format("{0:###.##}", 12394.039) |
12394.04 |
? |
|
string.Format("{0:####.#}", 194.039) |
194 |
? |
| ? | ? | ? |
|
日期时间 |
? | ? |
|
string.Format("{0:d}",System.DateTime.Now) |
2009-3-20 |
月份位置不是03 |
|
string.Format("{0:D}",System.DateTime.Now) |
2009年3月20日 |
? |
|
string.Format("{0:f}",System.DateTime.Now) |
2009年3月20日 15:37 |
? |
|
string.Format("{0:F}",System.DateTime.Now) |
2009年3月20日 15:37:52 |
? |
|
string.Format("{0:g}",System.DateTime.Now) |
2009-3-20 15:38 |
? |
|
string.Format("{0:G}",System.DateTime.Now) |
2009-3-20 15:39:27 |
? |
|
string.Format("{0:m}",System.DateTime.Now) |
3月20日 |
? |
|
string.Format("{0:t}",System.DateTime.Now) |
15:41 |
? |
|
string.Format("{0:T}",System.DateTime.Now) |
15:41:50 |
? |
|
string.Format("{0:r}",System.DateTime.Now) |
Sun, 17 Jun 2012 10:12:56 GMT |
RFC1123 日期格式 |
|
string.Format("{0:s}",System.DateTime.Now) |
2012-06-17T10:15:12 |
排序日期字符串 |
|
string.Format("{0:Y}",System.DateTime.Now) |
2012年6月 |
? |
|
string.Format("{0:U}",System.DateTime.Now) |
2012年6月17日 2:19:51 |
通用排序, GMT时间 |
|
string.Format("{0:u}",System.DateTime.Now) |
2012-06-17 10:19:51Z |
通用排序,本地时间 |
| ? | ? | ? |
|
日期时间(自定义) |
? | ? |
|
{0:dd} |
10 |
Day |
|
{0:ddd} |
Tue |
Day name |
|
{0:dddd} |
Tuesday |
Full day name |
|
{0:fff} |
932 |
毫秒 |
|
{0:gg} |
A.D. |
Era |
|
{0:hh} |
10 |
2 digit hour |
|
{0:HH} |
22 |
2 digit hour, 24hr format |
|
{0:mm} |
38 |
Minute 00-59 |
|
{0:MM} |
12 |
Month 01-12 |
|
{0:MMM} |
Dec |
Month abbreviation |
|
{0:MMMM} |
December |
月的名字 |
|
{0:ss} |
46 |
秒数从00到59 |
|
{0:tt} |
PM |
AM或PM(上午或下午) |
|
{0:yy} |
02 |
年(两位数字) |
|
{0:yyyy} |
2002 |
年 |
|
{0:zz} |
-05 |
Timezone offset, 2 digits |
|
{0:zzz} |
-05:00 |
Full timezone offset |
|
{0:hh:mm:ss} |
10:43:20 |
Separator |
|
{0:dd/MM/yyyy} |
10/12/2002 |
Separator |
| ? | ? | ? |
|
字符串 |
? | ? |
|
String.Format("->{0,10}<-", "Hello"); |
-> Hello<- |
至少10字符,不够前面(正数)补空 |
|
String.Format("->{0,-10}<-", "Hello") |
->Hello <- |
至少10字符,不够后面(负数)补空 |
| ? | ? | ? |
|
电话号码自定义 |
? | ? |
|
String.Format("{0:(###) ###-####}", 18005551212) |
(1800) 555-1212 |
? |
|
String.Format("{0:(##)####-########}", 86075487870252) |
(86)0754-87870252 |
? |
|
String.Format("{0:(##)####-########}", 086075487870252) |
(86)0754-87870252 |
? |
| ? | ? | ? |
|
零占位符”0” |
? | ? |
|
int i=120; |
00,120.00 |
? |
| ? | ? | ? |
|
数字占位符”#” |
? | ? |
|
double d=12050.8520; |
12,050.85 |
? |
| ? | ? | ? |
| ? | ? | ? |
版权声明:本文为博主原创文章,未经博主允许不得转载。
C# String.Format大全的更多相关文章
- C# String.Format大全 去 decimal 后面的 0
转 http://kwon.iteye.com/blog/1068255 http://blog.csdn.net/tvvbbb/article/details/47256943 public st ...
- string.format大全
字符串的数字格式 stringstr1 =string.Format("{0:N1}",56789); //result: 56,789.0 strin ...
- String.Format 大全
0.0的格式化 string.Format("{0:8D8}", 3)//第一个8表示空8个位置,后一个8表示用0填写最多8位数据 1.格式化货币(跟系统的环境有关,中文系统默认格 ...
- c# 字符串连接使用“+”和string.format格式化两种方式
参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...
- 【转】string.Format对C#字符串格式化
转自:http://blog.csdn.net/samsone/article/details/7556781 1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) str ...
- C#中string.format用法详解
C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...
- string.Format格式化用法详解
1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}",0.2) 结果为:¥0.20 (英文操作系统结果:$0 ...
- Js实现string.format
经常需要动态拼接html字符串,想到用类似于.net的string.format函数比较好,于是找了下,stackoverflow的代码: if (!String.prototype.format) ...
- String.Format用法
http://blog.csdn.net/yohop/article/details/2534907 1.作为参数 名称 说明 Format(String, Object) 将指定的 Stri ...
随机推荐
- ubuntu12.10可用更新源
ubutnu12.10自带的更新源已经失效,国内各大服务器的更新源,无论是网易.搜狐还是教育网的因此也失效了.附件是ubuntu12.10目前的可用更新源. 将地址中的“us.archive”换成“o ...
- OpenSessionInViewFilter与org.springframework.dao.InvalidDataAccessApiUsageException
报错:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in r ...
- 更改Keil工程名
假设原工程名为A,需要改成B. 1, 在工程目录下,把A.vuopt和A.uvproj改成B.uvopt和B.uvproj. 2,删除其他A文件. 3,打开工程B.然后修改下面位置: Project ...
- Pair Project: Elevator Scheduler [电梯调度算法的实现和测试]:思考题——谢勤政11061197
第一题: 大楼里面的电梯一般分区域,或考虑思考题第四题的情况,运行楼层不一样的电梯属于不同的区域.然后在接口IRequest和IPassenger还有IElevator里面都加上int area这个属 ...
- Java编程思想(2)之一切皆对象
- GC的代机制
1.代为CLR垃圾回收器采用的一种机制,基于以下几点假设:对象越新,生存期越短:对象越老,生存期越长: 2.托管堆仅支持3代(GC.MaxGeneration,第0代.第1代.第2代),CLR初始化会 ...
- oct(x) 将一个数字转化为8进制
>>> a = 12 >>> b 21 >>> c = oct(a) >>> d = oct(b) >>> c ...
- BZOJ 3996 [TJOI 2015] 线性代数 解题报告
首先,我们可以得到: $$D = \sum_{i=1}^{n}\sum_{j=1}^{n}a_i\times a_j\times b_{i,j} - \sum_{i=1}^{n}a_i\times c ...
- location.hash && location.href
hash:设置或获取 href 属性中在井号“#”后面的分段. href:设置或获取整个URL为字符串. 通过下面的测试你会发现区别,将代码放到你的HTML中,然后用浏览器打开,测试步骤: 点击“超链 ...
- 为 UWP 应用提供的 .NET 网络 API
[编者按]本文作者是 Windows networking 团队项目经理 Sidharth Nabar.在微软 Build 2015 大会上,.NET Core 5 作为开发 UWP(Universa ...