string format的各类格式及用法
数字 {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);
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
string format的各类格式及用法的更多相关文章
- C# String.Format用法和格式说明
1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}",0.2) 结果为:¥0.20 (英文操作系统结果:$0 ...
- 用DateTime.ToString(string format)输出不同格式的日期
http://www.cnblogs.com/xvqm00/archive/2009/02/19/1394093.html DateTime.ToString()函数有四个重载.一般用得多的就是不带参 ...
- [转]用DateTime.ToString(string format)输出不同格式的日期
DateTime.ToString()函数有四个重载.一般用得多的就是不带参数的那个了.殊不知,DateTime.ToString(string format)功能更强大,能输出不同格式的日期.以下把 ...
- string.Format 格式化日期格式
DateTime dt = DateTime.Now;//2010年10月4日 17点05分 string str = ""; //st ...
- c# string.format和tostring()
字符 说明 示例 输出 C 货币 string.Format("{0:C3}", 2) $2.000 D 十进制 string.Format("{0:D3}", ...
- C#中的String.Format方法(转)
一.定义String.Format是将指定的 String类型的数据中的每个格式项替换为相应对象的值的文本等效项. 如: (1)string p1 = "Jackie";strin ...
- String.Format() 方法
一 定义 String.Format() 是将指定的 String类型的数据中的每个格式项替换为相应对象的值的文本等效项. string p1 = "Jackie"; string ...
- C# String.Format方法
一.定义String.Format是将指定的 String类型的数据中的每个格式项替换为相应对象的值的文本等效项. 如: (1)string p1 = "Jackie";strin ...
- String.Format 大全
0.0的格式化 string.Format("{0:8D8}", 3)//第一个8表示空8个位置,后一个8表示用0填写最多8位数据 1.格式化货币(跟系统的环境有关,中文系统默认格 ...
随机推荐
- Python_range
range 当作定义的数字范围列表. 满足顾头不顾腚,可以加步长,与for循环结合使用. 一般使用 for i in range(0, 101): print(i) 结果: #[0,1,2,3,... ...
- JSON 的几种转换
package com.qbskj.project.util; import java.io.IOException; import java.io.Writer; import org.spring ...
- [百度百科]dir命令指定显示的排序方式
https://jingyan.baidu.com/article/7c6fb428dcf39880642c9095.html 今天工作中遇到了这个需求 感觉很好用 dir /o:d >name ...
- qsort.c源码
/* 版权所有(C) 1991-2019 自由软件资金会. 该文件属于是GUN C语言函数库,由Douglas C. Schmidt(schmidt@ics.uci.edu)所写. GUN C语言函数 ...
- 'cordova' 不是内部或外部命令,也不是可运行的程序
问题: CMD 'cordova' 不是内部或外部命令,也不是可运行的程序: 解决:配置环境变量 1.找到npm的安装路径 :如C:\Users\AppData\Roaming\npm 2.环境 ...
- 转载泡泡机器人——IMU预积分总结与公式推导2
本文为IMU预积分总结与公式推导系列技术报告的第二篇. 承接第一篇的内容,本篇将推导IMU预积分的测量值,并分析其测量误差的分布形式. 传统捷联惯性导航的递推算法,以初始状态为基础,利用IMU测量得到 ...
- SpringBoot配置MySql数据库和Druid连接池
1.pom文件增加相关依赖 <dependency> <groupId>mysql</groupId> <artifactId>mysql-connec ...
- python调试之pdb
一.PDB调试命令 pdb调试命令 完整命令 简写命令 描述 args a 列出当前函数的参数 break b <行号> 在某一行设置断点 break b <文件名>:< ...
- vue在html中出现{{}}原因及解决办法
在刚开始接触vue的时候,我们都是直接用<script>引入vue.js使用.没有借助vue-cli脚手架来构建项目. 对于一个初学者来说,跟着文档慢慢搬砖,使用vue进行数据绑定. 记得 ...
- 文本编辑利器Notepad++ 10个强大而又鲜为人知的特性【转】
文本编辑利器Notepad++ 10个强大而又鲜为人知的特性 - 为程序员服务