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 ...
随机推荐
- 实习笔记-2:sql 分组不一定要group by
今天在公司写代码的时候,遇到一个sql语句构建问题. 情形是这样的: 我需要获取不同小组下前N条记录. select top 10 * from dbo.Topic where GroupID in ...
- Linux 系统结构详解
Linux 系统结构详解 Linux系统一般有4个主要部分: 内核.shell.文件系统和应用程序.内核.shell和文件系统一起形成了基本的操作系统结构,它们使得用户可以运行程序.管理文件并使用系统 ...
- display:block; 块级元素。<a>,<span>标签设置宽度和高度
display:block;是让对象成为块级元素(比如a,span等) 转化后 可以对a或者span标签进行width和height设置,否则设置不了 display有很多对象,具体可以参考http: ...
- aspx页面状态管理Cookie和ViewState
Cookie 设置cookie protected void Button2_Click(object sender, EventArgs e) { HttpCookie cookie = new H ...
- 网页clientWidth等相关
javascript代码: function getInfo() { var s = ""; s += & ...
- django1.6之创建用户
如何你运行python manager.py syncdb(数据库同步成功) 则运行一下代码 >>> from django.contrib.auth.models import U ...
- sscanf用法简析
1. 常见用法. char buf[512] = ; sscanf("123456 ", "%s", buf); printf("%s\n" ...
- windows下STM32开发环境的搭建
一.概述 1.说明 笔者已经写了一篇Linux下STM32开发环境的搭建 ,这两篇文章的最区别在于开发环境所处的系统平台不一样,而其实这个区别对于开发环境的搭建其实影响不大,制作局部上的操作上发生了改 ...
- 【git】切换分支获取代码
Welcome to Git (version 1.9.5-preview20150319) Run 'git help git' to display the help index.Run 'git ...
- qt 5 小练习 创建无边框界面
我们大家都知道QT5 自带的界面不是那么美观,并且每个软件我们都发现他们的边框是自定义的,所以我决定写一篇这样的博文,也许已经有许许多多篇大牛写的论文了,但我还是想写一篇记录自己的学习QT的历程 首先 ...