c# ToString()格式大全(转)
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 |
货币 |
2.5.ToString("C") |
¥2.50 |
|
D |
十进制数 |
25.ToString("D5") |
00025 |
|
E |
科学型 |
25000.ToString("E") |
2.500000E+005 |
|
F |
固定点 |
25.ToString("F2") |
25.00 |
|
G |
常规 |
2.5.ToString("G") |
2.5 |
|
N |
数字 |
2500000.ToString("N") |
2,500,000.00 |
|
X |
十六进制 |
255.ToString("X") |
FF |
formatCode 是可选的格式化代码字符串。(详细内容请搜索“格式化字符串”查看)
必须用“{”和“}”将格式与其他字符分开。如果恰好在格式中也要使用大括号,可以用连续的两个大括号表示一个大括号,即: “{{”或者“}}”。
常用格式举例:
(1) int i=12345;
this.textBox1.Text=i.ToString();
//结果 12345(this指当前对象,或叫当前类的实例)
this.textBox2.Text=i.ToString("d8");
//结果 00012345
(2) int i=123;
double j=123.45;
string s1=string.Format("the value is {0,7:d}",i);
string s2=string.Format("the value is {0,7:f3}",j);
this.textBox1.Text=s1 ;
//结果 the value is 123
this.textBox2.Text=s2;
//结果 the value is 123.450
(3)double i=12345.6789;
this.textBox1.Text=i.ToString("f2"); //结果 12345.68
this.textBox2.Text=i.ToString("f6");
//结果 12345.678900
(4)double i=12345.6789;
this.textBox1.Text=i.ToString("n"); //结果 12,345.68
this.textBox2.Text=i.ToString(“n4”); //结果 12,345.6789
(5)double i=0.126;
string s=string.Format("the value is {0:p}",i);
this.textBox1.Text=i.ToString("p"); //结果 12.6%
this.textBox2.Text=s; //结果 the value is 12.6%
(6) DateTime dt =new DateTime(2003,5,25);
this.textBox1.Text=dt.ToString("yy.M.d");
//结果 03.5.25
this.textBox2.Text=dt.ToString(“yyyy年M月”);
//结果 2003年5月
Convert.ToDateTime("2005/12/22 22:22:22").ToString("yyyy/MM/dd HH:mm:ss")
"2005/12/22 22:22:22"
(7) int i=123;
double j=123.45;
string s=string.Format("i:{0,-7},j:{1,7}",i,j);
//-7表示左对齐,占7位
this.textBox1.Text=s ;
//结果i:123 ,j: 123.45
c# ToString()格式大全(转)的更多相关文章
- C#.ToString()格式大全
原文:C#.ToString()格式大全 C#.ToString()格式大全 stringstr1=string.Format("{0:N1}",56789); ...
- C#中ToString格式大全
更多资源:http://denghejun.github.io C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5&q ...
- C# ToString格式大全
C# 货币 2.5.ToString("C"); // ¥2.50 // D 10进制数 25.ToString("D5"); // 25000 // E ...
- C#中的ToString格式大全
// C# 日期格式 DateTime dt = DateTime.Now; dt.ToString();//2005-11-5 13:21:25 dt.ToFileTime().ToString() ...
- C#中ToString和Formate格式大全
C#中ToString格式大全 stringstr1 =); //result: 56,789.0 stringstr2 =); //result: 56,789.00 stringstr3 =); ...
- C#时间日期格式大全
C#时间/日期格式大全,C#时间/日期函数大全 有时候我们要对时间进行转换,达到不同的显示效果 默认格式为:2016-7-1 14:33:34 如果要换成成201607,07-2016,2016-7- ...
- asp.net C# 时间格式大全
asp.net C# 时间格式大全DateTime dt = DateTime.Now;// Label1.Text = dt.ToString();//2005-11-5 13:21:25// ...
- java日期格式大全 format SimpleDateFormat(转)
java日期格式大全 format SimpleDateFormat /** * 字符串转换为java.util.Date<br> * 支持格式为 yyyy.MM.dd G ...
- Java日期时间API系列12-----Jdk8中java.time包中的新的日期时间API类,日期格式化,常用日期格式大全
通过Java日期时间API系列10-----Jdk8中java.time包中的新的日期时间API类的DateTimeFormatter, 可以看出java8的DateTimeFormatter完美解决 ...
随机推荐
- Linux命令篇-服务器查看日志(续)
此文是继上文如何在服务器看日志的续集.之所以我觉得自己很菜,是因为我的周围都是大佬,他们都是值得我学习的对象. 通常大家看日志,无非两种,一是more命令,一是tail命令,其中的过程要么是翻页查看, ...
- NX二次开发-UFUN打开本地文本文档uc4504
NX9+VS2012 #include <uf.h> #include <uf_cfi.h> #include <uf_ui.h> using std::strin ...
- phpstorm使用说明
1.phpstorm软件可以直接断点调试php代码.具体配置方法参考 http://blog.csdn.net/qq4551091/article/details/55258664 就可以了,不过只要 ...
- Rubber Ducky简介
USB Rubber Ducky是一款模仿人工键盘输入的设备,外形和U盘一样,模拟键盘输入速度可达到1000个字符每分钟,并且适合任何操作系统,包括安卓等移动OS,它使用的是它特定的脚本语言,用记事本 ...
- 清理Visual Studio解决方案临时文件:Clean Visual Studio Solution Temporary File Build20160418
复制保存到任意文件名.bat,放置在Visual Studio Solution目录下. 当Visual Studio Solution目录过于庞大或打算拷贝移动Visual Studio Solut ...
- web开发调用百度地图API + AK申请
web开发调用百度地图API + AK申请 要使用百度地图的API我们首先需要在我们的html页面引入js----`` 如何获取百度地图ak: 1.登陆百度地图开发者平台 2.注册百度开发者账号并登陆 ...
- D3.js绘制 颜色:RGB、HSL和插值 (V3版本)
颜色和插值 计算机中的颜色,常用的标准有RGB和HSL. RGB:色彩模式是通过对红(Red).绿(Green).蓝(Blue)三个颜色通道相互叠加来得到额各式各样的颜色.三个通道的值得范围都 ...
- [工具]Editplus添加son格式化支持
EditPlus安装包和json.js文件地址 不喜欢CSDN的积分下载和登录下载,不喜欢百度网盘,就这么倔强 https://github.com/michael-deve/CommonData-E ...
- Mysql优化-典型的服务器配置
内存配置相关参数 mysql内存分配需要考虑到操作系统需要使用的内存,其他应用程序所要使用的内存,mysql的会话数以及每个会话使用的内存,然后就是操作系统实例所使用的内存.生产环境的mysql往往都 ...
- Codeforces 1151E 统计贡献
题意:给你一个数组a,设函数f(l, r)为数组a中权值在[l, r]之间的连通块的数目,比如a = [1, 3, 2, 1], f(1, 2) = 2, 连通块是位置1和位置3,4.问Σ(i = 1 ...