c#字符显示转换{0:d} string.Format()
这一篇实际和前几个月写的没什么本质上的区别。但是这篇更明确一点,学起来easy
c#字符显示转换{0:d}
C#:String.Format数字格式化输出 :
int a = 12345678;
//格式为sring输出
// Label1.Text = string.Format("asdfadsf{0}adsfasdf",a);
// Label2.Text = "asdfadsf"+a.ToString()+"adsfasdf";
// Label1.Text = string.Format("asdfadsf{0:C}adsfasdf",a);//asdfadsf¥1,234.00adsfasdf
// Label2.Text = "asdfadsf"+a.ToString("C")+"adsfasdf";//asdfadsf¥1,234.00adsfasdf
double b = 1234.12543;
a = 12345678;
//格式为特殊的string样式输出
// Label1.Text = string.Format("asdfadsf{0:C}adsfasdf",b);//asdfadsf¥1,234.13adsfasdf
// Label2.Text = "asdfadsf"+b.ToString("C")+"adsfasdf";//asdfadsf¥1,234.13adsfasdf
// Label1.Text = string.Format("{0:C3}",b);//¥1,234.125
// Label2.Text = b.ToString("C3");//¥1,234.125
// Label1.Text = string.Format("{0:d}",a);//十进制--12345678
// Label2.Text = b.ToString("d");//十进制--相同的类型,转换报错
// Label1.Text = string.Format("{0:e}",a);//指数--1.234568e+007
// Label2.Text = b.ToString("e");//指数--1.234125e+003
// Label1.Text = string.Format("{0:f}",a);//定点数--12345678.00
// Label2.Text = b.ToString("f");//定点数--1234.13
// Label1.Text = string.Format("{0:n}",a);//数值--12,345,678.00
// Label2.Text = b.ToString("n");//数值--1,234.13
// Label1.Text = string.Format("{0:x}",a);//十六进制--bc614e
// Label2.Text = b.ToString("x");//16--带有小数不能转换,出错
// Label1.Text = string.Format("{0:g}",a);//通用为最紧凑--12345678
// Label2.Text = b.ToString("g");//通用为最紧凑--1234.12543
// Label1.Text = string.Format("{0:r}",a);//转来转去不损失精度--整数不允许用,报错
// Label2.Text = b.ToString("r");//转来转去不损失精度--1234.12543
b = 4321.12543;
a = 1234;
//自定义模式输出:
// 0 描述:占位符,如果可能,填充位
// Label1.Text = string.Format("{0:000000}",a);// 001234
// Label2.Text = string.Format("{0:000000}",b);// 004321
// # 描述:占位符,如果可能,填充位
// Label1.Text = string.Format("{0:#######}",a);// 1234
// Label2.Text = string.Format("{0:#######}",b);// 4321
// Label1.Text = string.Format("{0:#0####}",a);// 01234
// Label2.Text = string.Format("{0:0#0000}",b);// 004321
// . 描述:小数点
// Label1.Text = string.Format("{0:000.000}",a);//1234.000
// Label2.Text = string.Format("{0:000.000}",b);//4321.125
b = 87654321.12543;
a = 12345678;
// , 描述:数字分组,也用于增倍器
// Label1.Text = string.Format("{0:0,00}",a);// 12,345,678
// Label2.Text = string.Format("{0:0,00}",b);// 87,654,32
// Label1.Text = string.Format("{0:0,}",a);// 12346
// Label2.Text = string.Format("{0:0,}",b);// 87654
// Label1.Text = string.Format("{0:0,,}",a);// 12
// Label2.Text = string.Format("{0:0,,}",b);// 88
// Label1.Text = string.Format("{0:0,,,}",a);// 0
// Label2.Text = string.Format("{0:0,,,}",b);// 0
// % 描述:格式为百分数
// Label1.Text = string.Format("{0:0%}",a);// 1234567800%
// Label2.Text = string.Format("{0:#%}",b);// 8765432113%
// Label1.Text = string.Format("{0:0.00%}",a);// 1234567800.00%
// Label2.Text = string.Format("{0:#.00%}",b);// 8765432112.54%
// 'abc' 描述:显示单引号内的文本
// Label1.Text = string.Format("{0:'文本'0}",a);// 文本12345678
// Label2.Text = string.Format("{0:文本 0}",b);// 文本87654321
// / 描述: 后跟1要打印字的字符,也用于转移符/n等
// Label1.Text = string.Format("/"你好! /"");// "你好!"
// Label2.Text = string.Format("//c//books//new//we.asp");///c/books/new/we.asp
// @描述:后跟要打印字的字符,
// Label1.Text = string.Format(@"""你好!"""); // "你好!"要打印"则需要输入两对才可以
// Label2.Text = string.Format(@"/c/books/new/we.asp");///c/books/new/we.asp
百分数格式应该用“p”这个参数。
格式 原始 数据 结 果
"{0:P}" 0.40 40%
数字 {0:N2} 12.36
数字 {0:N0} 13
货币 {0:c2} $12.36
货币 {0:c4} $12.3656
货币 "¥{0:N2}" ¥12.36
科学计数法 {0:E3} 1.23E+001
百分数 {0:P} 12.25% P and p present the same.
日 期 {0:D} 2006年11月25日
日期 {0:d} 2006-11-25
日期 {0:f} 2006年11月25日 10:30
日期 {0:F} 2006年11月25日 10:30:00
日期 {0:s} 2006-11-26 10:30:00
时间 {0:T} 10:30:00
DateTime dt = DateTime.Now;
Label1.Text = dt.ToString();//2005-11-5 13:21:25
Label2.Text = dt.ToFileTime().ToString();//127756416859912816
Label3.Text = dt.ToFileTimeUtc().ToString();//127756704859912816
Label4.Text = dt.ToLocalTime().ToString();//2005-11-5 21:21:25
Label5.Text = dt.ToLongDateString().ToString();//2005年11月5日
Label6.Text = dt.ToLongTimeString().ToString();//13:21:25
Label7.Text = dt.ToOADate().ToString();//38661.5565508218
Label8.Text = dt.ToShortDateString().ToString();//2005-11-5
Label9.Text = dt.ToShortTimeString().ToString();//13:21
Label10.Text = dt.ToUniversalTime().ToString();//2005-11-5 5:21:25
Label1.Text = dt.Year.ToString();//2005
Label2.Text = dt.Date.ToString();//2005-11-5 0:00:00
Label3.Text = dt.DayOfWeek.ToString();//Saturday
Label4.Text = dt.DayOfYear.ToString();//309
Label5.Text = dt.Hour.ToString();//13
Label6.Text = dt.Millisecond.ToString();//441
Label7.Text = dt.Minute.ToString();//30
Label8.Text = dt.Month.ToString();//11
Label9.Text = dt.Second.ToString();//28
Label10.Text = dt.Ticks.ToString();//632667942284412864
Label11.Text = dt.TimeOfDay.ToString();//13:30:28.4412864
Label1.Text = dt.ToString();//2005-11-5 13:47:04
Label2.Text = dt.AddYears(1).ToString();//2006-11-5 13:47:04
Label3.Text = dt.AddDays(1.1).ToString();//2005-11-6 16:11:04
Label4.Text = dt.AddHours(1.1).ToString();//2005-11-5 14:53:04
Label5.Text = dt.AddMilliseconds(1.1).ToString();//2005-11-5 13:47:04
Label6.Text = dt.AddMonths(1).ToString();//2005-12-5 13:47:04
Label7.Text = dt.AddSeconds(1.1).ToString();//2005-11-5 13:47:05
Label8.Text = dt.AddMinutes(1.1).ToString();//2005-11-5 13:48:10
Label9.Text = dt.AddTicks(1000).ToString();//2005-11-5 13:47:04
Label10.Text = dt.CompareTo(dt).ToString();//0
Label11.Text = dt.Add(?).ToString();//问号为一个时间段
Label1.Text = dt.Equals("2005-11-6 16:11:04").ToString();//False
Label2.Text = dt.Equals(dt).ToString();//True
Label3.Text = dt.GetHashCode().ToString();//1474088234
Label4.Text = dt.GetType().ToString();//System.DateTime
Label5.Text = dt.GetTypeCode().ToString();//DateTime
Label1.Text = dt.GetDateTimeFormats('s')[0].ToString();//2005-11-05T14:06:25
Label2.Text = dt.GetDateTimeFormats('t')[0].ToString();//14:06
Label3.Text = dt.GetDateTimeFormats('y')[0].ToString();//2005年11月
Label4.Text = dt.GetDateTimeFormats('D')[0].ToString();//2005年11月5日
Label5.Text = dt.GetDateTimeFormats('D')[1].ToString();//2005 11 05
Label6.Text = dt.GetDateTimeFormats('D')[2].ToString();//星期六 2005 11 05
Label7.Text = dt.GetDateTimeFormats('D')[3].ToString();//星期六 2005年11月5日
Label8.Text = dt.GetDateTimeFormats('M')[0].ToString();//11月5日
Label9.Text = dt.GetDateTimeFormats('f')[0].ToString();//2005年11月5日 14:06
Label10.Text = dt.GetDateTimeFormats('g')[0].ToString();//2005-11-5 14:06
Label11.Text = dt.GetDateTimeFormats('r')[0].ToString();//Sat, 05 Nov 2005 14:06:25 GMT
Label1.Text = string.Format("{0:d}",dt);//2005-11-5
Label2.Text = string.Format("{0:D}",dt);//2005年11月5日
Label3.Text = string.Format("{0:f}",dt);//2005年11月5日 14:23
Label4.Text = string.Format("{0:F}",dt);//2005年11月5日 14:23:23
Label5.Text = string.Format("{0:g}",dt);//2005-11-5 14:23
Label6.Text = string.Format("{0:G}",dt);//2005-11-5 14:23:23
Label7.Text = string.Format("{0:M}",dt);//11月5日
Label8.Text = string.Format("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT
Label9.Text = string.Format("{0:s}",dt);//2005-11-05T14:23:23
Label10.Text string.Format("{0:t}",dt);//14:23
Label11.Text = string.Format("{0:T}",dt);//14:23:23
Label12.Text = string.Format("{0:u}",dt);//2005-11-05 14:23:23Z
Label13.Text = string.Format("{0:U}",dt);//2005年11月5日 6:23:23
Label14.Text = string.Format("{0:Y}",dt);//2005年11月
Label15.Text = string.Format("{0}",dt);//2005-11-5 14:23:23
Label16.Text = string.Format("{0:yyyyMMddHHmmssffff}",dt);
stringstr1 =string.Format("{0:N1}",56789); //result: 56,789.0
stringstr2 =string.Format("{0:N2}",56789); //result: 56,789.00
stringstr3 =string.Format("{0:N3}",56789); //result: 56,789.000
stringstr8 =string.Format("{0:F1}",56789); //result: 56789.0
stringstr9 =string.Format("{0:F2}",56789); //result: 56789.00
stringstr11 =(56789 / 100.0).ToString("#.##"); //result: 567.89
stringstr12 =(56789 / 100).ToString("#.##"); //result: 567
C 或 c
货币
Console.Write("{0:C}", 2.5); //$2.50
Console.Write("{0:C}", -2.5); //($2.50)
D 或 d
十进制 数
Console.Write("{0:D5}", 25); //00025
E 或 e
科学型
Console.Write("{0:E}", 250000); //2.500000E+005
F 或 f
固定点
Console.Write("{0:F2}", 25); //25.00
Console.Write("{0:F0}", 25); //25
G 或 g
常规
Console.Write("{0:G}", 2.5); //2.5
N 或 n
数字
Console.Write("{0:N}", 2500000); //2,500,000.00
X 或 x
十六进制
Console.Write("{0:X}", 250); //FA
Console.Write("{0:X}", 0xffff); //FFFF
c#字符显示转换{0:d} string.Format()的更多相关文章
- c#字符显示转换{0:d}
C#:String.Format数字格式化输出 : int a = 12345678; //格式为sring输出// Label1.Text = string.Format("asdfads ...
- 【转】C# String.Format数字格式化输出各种转换{0:N2} {0:D2} {0:C2}...
; //格式为sring输出 // Label1.Text = string.Format("asdfadsf{0}adsfasdf",a); // Label2.Text = & ...
- C#:String.Format数字格式化输出 {0:N2} {0:D2} {0:C2}等等
int a = 12345678; //格式为sring输出// Label1.Text = string.Format("asdfadsf{0}adsfasdf",a); / ...
- String.Format数字格式化输出 {0:N2} {0:D2} {0:C2
//格式为sring输出 // Label1.Text = string.Format("asdfadsf{0}adsfasdf",a); // Label2.Text = ...
- C#:String.Format数字格式化输出 {0:N2} {0:D2} {0:C2}...
int a = 12345678; //格式为sring输出// Label1.Text = string.Format("asdfadsf{0}adsfasdf",a);// ...
- String.Format数字格式化输出 {0:N2} {0:D2} {0:C2} (转)
String.Format数字格式化输出 {:N2} {:D2} {:C2} (转) //格式为sring输出 // Label1.Text = string.Format("asdfads ...
- String.Format用法
http://blog.csdn.net/yohop/article/details/2534907 1.作为参数 名称 说明 Format(String, Object) 将指定的 Stri ...
- C#:String.Format数字格式化输出
int a = 12345678; //格式为sring输出// Label1.Text = string.Format("asdfadsf{0}adsfasdf",a);// ...
- .net String.Format数字格式化输出
内容转载自:http://www.cnblogs.com/lqb/archive/2008/08/04/1259498.html 前面内容这个做的总结的很全,今后有新增的我继续往后补充.请留意我增加的 ...
随机推荐
- WCF:无法满足对安全令牌的请求,因为身份验证失败。
服务端和客户端如果有认证的话的这样: <wsHttpBinding> <binding name="WSHttpBinding_IService1" closeT ...
- 在 CentOS7 上安装 swftools
1.从官网下载 swftools,这里下载的是 0.9.2 版本: wget http://www.swftools.org/swftools-0.9.2.tar.gz 2.下载后得到 swftool ...
- 织梦上传webp格式图片
织梦cms如何添加其他格式的图片呢? 这里以webp为类来实现一下如何让织梦支持webp 1.修改系统设置让织梦(dede)支持该图片格式,比如:webp(新图片格式) 系统->系统基本参数-& ...
- Ubuntu、Windows 、Linux集合
一.Ubuntu/Windows双系统修复引导 首先说明:在Windows存在的前提下安装Ubuntu(或者Ubuntu系列)是不需要修复引导的.因为grub会自动搜索存在硬盘中的系统. 而在 ...
- 用SQL将数字转换为中文数字
IF OBJECT_ID('fn_GetChnNum') IS NOT NULL BEGIN DROP FUNCTION dbo.fn_GetChnNum; END; GO CREATE FUNCTI ...
- 10大炫酷的HTML5文字动画特效欣赏
文字是网页中最基本的元素,在CSS2.0时代,我们只能在网页上展示静态的文字,只能改变他的大小和颜色,显得枯燥无味.随着HTML5的发展,现在网页中的文字样式变得越来越丰富了,甚至出现了文字动画,HT ...
- 2016 Al-Baath University Training Camp Contest-1
2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...
- 【[SDOI2017]新生舞会】
题目 好题啊 我们要求的是 \[C=\frac{\sum_{i=1}^na_i}{\sum_{i=1}^nb_i}\] 使得\(C\)最大 显然 \[C\times \sum_{i=1}^nb_i=\ ...
- php无法保存SESSION问题总汇
昨天客户又过来说网站的问题,说的也都是些毛毛雨的东西,管理那么多网站,再有这么些客户的存在,本人也是累了,但当登录后台的时候突然发现后台登录不了,查看了一下验证码服务器端的session为空值,之前登 ...
- clear:both;和overflow:hidden;的应用理解。
摘自cbwcwy 前辈: clear是子模块之间限定的,如下:<div id="a"> <div id="1"></div& ...