C#-ToString格式化
Int.ToString(format):
格式字符串采用以下形式:Axx,其中 A 为格式说明符,指定格式化类型,xx 为精度说明符,控制格式化输出的有效位数或小数位数,具体如下:
格式说明符
| 说明 | 示例 | 输出 | |
| 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 |
DateTime.ToString(format):
以下格式只能单独使用,表示特定的格式:
- d ShortDatePattern
- D LongDatePattern
- f 完整日期和时间(长日期和短时间)
- F FullDateTimePattern(长日期和长时间)
- g 常规(短日期和短时间)
- G 常规(短日期和长时间)
- m、M MonthDayPattern
- r、R RFC1123Pattern
- s 使用当地时间的 SortableDateTimePattern(基于 ISO 8601)
- t ShortTimePattern
- T LongTimePattern
- u UniversalSortableDateTimePattern 用于显示通用时间的格式
- U 使用通用时间的完整日期和时间(长日期和长时间)
- y、Y YearMonthPattern
以下格式可以组合使用,格式化出不同的日期显示格式:
- d 月中的某一天。一位数的日期没有前导零。
- dd 月中的某一天。一位数的日期有一个前导零。
- ddd 周中某天的缩写名称,在 AbbreviatedDayNames 中定义。
- dddd 周中某天的完整名称,在 DayNames 中定义。
- M 月份数字。一位数的月份没有前导零。
- MM 月份数字。一位数的月份有一个前导零。
- MMM 月份的缩写名称,在 AbbreviatedMonthNames 中定义。
- MMMM 月份的完整名称,在 MonthNames 中定义。
- y 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示不具有前导零的年份。
- yy 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。
- yyyy 包括纪元的四位数的年份。
- gg 时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。
- h 12 小时制的小时。一位数的小时数没有前导零。
- hh 12 小时制的小时。一位数的小时数有前导零。
- H 24 小时制的小时。一位数的小时数没有前导零。
- HH 24 小时制的小时。一位数的小时数有前导零。
- m 分钟。一位数的分钟数没有前导零。
- mm 分钟。一位数的分钟数有一个前导零。
- s 秒。一位数的秒数没有前导零。
- ss 秒。一位数的秒数有一个前导零。
- f 秒的小数精度为一位。其余数字被截断。
- ff 秒的小数精度为两位。其余数字被截断。
- fff 秒的小数精度为三位。其余数字被截断。
- ffff 秒的小数精度为四位。其余数字被截断。
- fffff 秒的小数精度为五位。其余数字被截断。
- ffffff 秒的小数精度为六位。其余数字被截断。
- fffffff 秒的小数精度为七位。其余数字被截断。
- t 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项的第一个字符(如果存在)。
- tt 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项(如果存在)。
- z 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数没有前导零。例如,太平洋标准时间是“-8”。
- zz 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数有前导零。例如,太平洋标准时间是“-08”。
- zzz 完整时区偏移量(“+”或“-”后面跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-08:00”。
- : 在 TimeSeparator 中定义的默认时间分隔符。
- / 在 DateSeparator 中定义的默认日期分隔符。
- % c 其中 c 是格式模式(如果单独使用)。如果格式模式与原义字符或其他格式模式合并,则可以省略“%”字符。
- " c 其中 c 是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“""”。
DateTime.ToString() 函数有四个重载。一般用得多的就是不带参数的那个了。殊不知, DateTime.ToString(string format) 功能更强大,能输出不同格式的日期。以下把一些情况罗列出来,供大家参考。有些在 MSDN 上有的就没有列出来了。
1. y 代表年份,注意是小写的 y ,大写的Y并不代表年份。
2. M 表示月份。
3. d 表示日期,注意 D 并不代表什么。
4. h 或 H 表示小时, h 用的是 12 小时制, H 用的是 24 小时制。
5. m 表示分钟。
6. s 表示秒。注意 S 并不代表什么。
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
在String.Format中使用格式化:
在String.Format方法中也可以格式化这些结构对象,这些格式化的格式包含在“{}”之间,用“:”隔开。“:”前面为String.Format的索引值,和一般格式化一样,而“:”后面则是这些结构的格式化类型。如果使用占位符,则和索引之间使用“,”分割,正数表示右对齐,负数表示左对齐,而绝对值表示所占的字符宽度数,例如:
//结果 the value is 123.450
其中:7表示占位符(右对齐),占7位,如果使用-7则表示左对齐,占七位。
部分内容转载自:
http://www.cnblogs.com/huangting2009/archive/2009/02/09/1386596.html
:first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: 0; } img { border: 0; max-width: 100%; height: auto !important; margin: 2px 0; } table { border-collapse: collapse; border: 1px solid #bbbbbb; } td, th { padding: 4px 8px; border-collapse: collapse; border: 1px solid #bbbbbb; } @media only screen and (-webkit-max-device-width: 1024px), only screen and (-o-max-device-width: 1024px), only screen and (max-device-width: 1024px), only screen and (-webkit-min-device-pixel-ratio: 3), only screen and (-o-min-device-pixel-ratio: 3), only screen and (min-device-pixel-ratio: 3) { html, body { font-size: 17px; } body { line-height: 1.7; padding: 0.75rem 0.9375rem; color: #353c47; } h1 { font-size: 2.125rem; } h2 { font-size: 1.875rem; } h3 { font-size: 1.625rem; } h4 { font-size: 1.375rem; } h5 { font-size: 1.125rem; } h6 { color: inherit; } ul, ol { padding-left: 2.5rem; } blockquote { padding: 0 0.9375rem; } }
-->
div{font-size:15px;}.wiz-table-tools .wiz-table-menu-item.active .wiz-table-menu-sub {display: block}.wiz-table-tools .wiz-table-menu-sub:before, .wiz-table-tools .wiz-table-menu-sub:after {position: absolute;content: " ";border-style: solid;border-color: transparent;border-bottom-color: #cccccc;left: 22px;margin-left: -14px;top: -8px;border-width: 0 8px 8px 8px;z-index:10;}.wiz-table-tools .wiz-table-menu-sub:after {border-bottom-color: #ffffff;top: -7px;}.wiz-table-tools .wiz-table-menu-sub-item {padding: 4px 12px;font-size: 14px;}.wiz-table-tools .wiz-table-menu-sub-item.split {border-top: 1px solid #E0E0E0;}.wiz-table-tools .wiz-table-menu-sub-item:hover {background-color: #ececec;}.wiz-table-tools .wiz-table-menu-sub-item.disabled {color: #bbbbbb;cursor: default;}.wiz-table-tools .wiz-table-menu-sub-item.disabled:hover {background-color: transparent;}.wiz-table-tools .wiz-table-menu-item.wiz-table-cell-bg:hover .wiz-table-color-pad {display: block;}.wiz-table-tools .wiz-table-color-pad {display: none;padding: 10px;box-sizing: border-box;width: 85px;height: 88px;background-color: #fff;cursor: default;}.wiz-table-tools .wiz-table-color-pad > div{font-size:15px;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item {display: inline-block;width: 15px;height: 15px;margin-right: 9px;position: relative;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item i.pad-demo {position: absolute;top:3px;left:0;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item .icon-oblique_line{color: #cc0000;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item:last-child {margin-right: 0;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item.active i.editor-icon.icon-box {color: #448aff;}.wiz-table-tools .wiz-table-cell-align {display: none;padding: 10px;box-sizing: border-box;width: 85px;height: 65px;background-color: #fff;cursor: default;}.wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item {display: inline-block;width: 15px;height: 15px;margin-right: 9px;position: relative;}.wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item:last-child {margin-right:0}.wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item i.valign{position: absolute;top:3px;left:0;color: #d2d2d2;}.wiz-table-tools .wiz-table-cell-align-item.active i.editor-icon.valign {color: #a1c4ff;}.wiz-table-tools .wiz-table-cell-align-item.active i.editor-icon.icon-box,.wiz-table-tools .wiz-table-cell-align-item.active i.editor-icon.align {color: #448aff;}.wiz-table-tools .wiz-table-color-pad .wiz-table-color-pad-item:last-child,.wiz-table-tools .wiz-table-cell-align .wiz-table-cell-align-item:last-child {margin-right: 0;}th.wiz-selected-cell-multi, td.wiz-selected-cell-multi {background: rgba(0,102,255,.05);}th:before,td:before,#wiz-table-col-line:before,#wiz-table-range-border_start_right:before,#wiz-table-range-border_range_right:before{content: " ";position: absolute;top: 0;bottom: 0;right: -5px;width: 9px;cursor: col-resize;background: transparent;z-index:100;}th:after,td:after,#wiz-table-row-line:before,#wiz-table-range-border_start_bottom:before,#wiz-table-range-border_range_bottom:before{content: " ";position: absolute;left: 0;right: 0;bottom: -5px;height: 9px;cursor: row-resize;background: transparent;z-index:100;}.wiz-table-container {}.wiz-table-body {position:relative;padding:0 0 10px;overflow-x:auto;overflow-y:hidden;-webkit-overflow-scrolling:touch;}.wiz-table-body table {margin:0;outline:none;}td,th {height:28px;word-break:break-all;box-sizing:border-box;outline:none;}body pre.prettyprint {padding:0;}body pre.prettyprint code {white-space: pre;}body pre.prettyprint.linenums {box-shadow:none; overflow: auto;-webkit-overflow-scrolling: touch;}body pre.prettyprint.linenums ol.linenums {box-shadow: 40px 0 0 #FBFBFC inset, 41px 0 0 #ECECF0 inset; padding: 10px 10px 10px 40px !important;}
-->
C#-ToString格式化的更多相关文章
- C# .ToString() 格式化
c# ToString() 格式化字符串 格式化数值:有时,我们可能需要将数值以一定的格式来呈现,就需要对数值进行格式化.我们使用格式字符串指定格式.格式字符串采用以下形式:Axx,其中 A 为格式 ...
- DateTime.ToString格式化日期,使用DateDiff方法获取日期时间的间隔数
一:DateTime.ToString格式化日期 二:代码 using System; using System.Collections.Generic; using System.Component ...
- ToString() 格式化
c# ToString() 格式化字符串 格式化数值:有时,我们可能需要将数值以一定的格式来呈现,就需要对数值进行格式化.我们使用格式字符串指定格式.格式字符串采用以下形式:Axx,其中 A 为格式 ...
- asp.net ToString() 格式化字符串
c# ToString() 格式化字符串 格式化数值:有时,我们可能需要将数值以一定的格式来呈现,就需要对数值进行格式化.我们使用格式字符串指定格式.格式字符串采用以下形式:Axx,其中 A 为格式 ...
- C# Tostring 格式化输出字符串全解
C 货币 2.5.ToString("C") ¥2.50 D 十进制数 .ToString("D5") E 科学型 .ToString("E" ...
- .tostring()格式化大全
C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5") 00025 E 科学型 25000.ToString ...
- tostring格式化输出
C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5") 00025 E 科学型 25000.ToString ...
- C# tostring 格式化输出 (转)
C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5") 00025 E 科学型 25000.ToString ...
- C#中string.Format()和ToString()格式化方法
C#数字格式化输出是我们在编程中经常需要处理的事情,那么这里向你介绍了一些C#数字格式化输出的例子,这样就会方便你来选择和比较,什么方式是比较适合自己项目的. int a = 12345678; C# ...
- [C#]ToString("##")格式化用法案例一:自动编码单据流水码
之前的写的自动编码单据流水码是写在存储过程或者函数中的,今天用程序写一个发现TOSTRING可以格式化方便. /// <summary> /// 年月日+两位流水码 /// </su ...
随机推荐
- B - Cube HDU - 1220 (数学计数)
题意:一个边长为N的正方体,切割成N*N*N个单位正方体,问有多少对正方体之间有0个,2个公共点. 思路:因为正方体之间出现公共点的情况有0,2,4. 那么直接正面求,肯定不好求,那么先求出有4个公共 ...
- Arduino IDE for ESP8266 项目(1) 点亮灯+按键LED+pwm
官方文档 http://esp8266.github.io/Arduino/versions/2.1.0/doc/libraries.html 引脚口说明 http://yfrobot.com/thr ...
- Qt 编程指南 3 信号和槽沟通
https://qtguide.ustclug.org/ 1 信号和槽 所谓信号槽,简单来说,就像是插销一样:一个插头和一个插座.怎么说呢?当某种事件发生之后,比如,点击了一下鼠标,或者按了某个按键, ...
- ethereum/EIPs-191 Signed Data Standard
https://github.com/ethereum/EIPs/blob/master/EIPS/eip-191.md eip title author status type category c ...
- oracle 索引的几种方式
一.查询索引的高度 select index_name,blevel,leaf_blocks,num_rows,distinct_keys,clustering_factorfrom user_ind ...
- Java的快速失败和安全失败
文章转自https://www.cnblogs.com/ygj0930/p/6543350.html 一:快速失败(fail—fast) 在用迭代器遍历一个集合对象时,如果遍历过程中对集合对象的内容进 ...
- sparse 稀疏函数的用法2
sparse函数 功能:Create sparse matrix-创建稀疏矩阵 用法1:S=sparse(X)——将矩阵X转化为稀疏矩阵的形式,即矩阵X中任何零元素去除,非零元素及其下标(索引)组成矩 ...
- springzuul本地路由和跨服务器路由问题
阿里云服务器在旧新服务迁移过程中,发现路由到认证中心找不到服务 解决办法: 在路由配置里面使用下面的配置 #zuul.routes.claimconf.path=/claimconf/**#zuul. ...
- xpath 的使用
如需转发,请注明出处:小婷儿的python https://www.cnblogs.com/xxtalhr/p/10520271.html 有问题请在博客下留言或加作者微信:tinghai87605 ...
- Redis 安装部署
1. 官网(https://redis.io/download)下载稳定版安装包 3.0.7或3.2或4.1; 2. 复制到部署服务器 /opt/redis4,解压 tar zxvf redis-4. ...