关键字:C# string.format
作者:txw1958
原文:http://www.cnblogs.com/txw1958/archive/2012/11/15/csharp-string_format.html

 

字符串

示例

输出

String.Format("->{1,10}<-", "Hello");

-> Hello<-

String.Format("->{1,-10}<-", "Hello");

->Hello <-

 

数字

基本数字格式示例:

字符

类型

示例

输出
(Double 1.42)

输出
(Int -12400)

c

Currency

货币

{0:c}

$1.42

-$12,400

d

Decimal (Whole number)

十进制

{0:d}

System.
FormatException

-12400

e

Scientific

科学记数法

{0:e}

1.420000e+000

-1.240000e+004

f

Fixed point

固定精度

{0:f}

1.42

-12400.00

g

General

常规

{0:g}

1.42

-12400

n

Number with commas for thousands

用逗号隔开的数字

{0:n}

1.42

-12,400

r

Round trippable

双向转换

{0:r}

1.42

System.
FormatException

x

Hexadecimal

十六进制

{0:x4}

System.
FormatException

cf90

自定义数字格式:

字符

类型

示例

输出

(Double 1500.42)

Note

0

Zero placeholder

0占位符

{0:00.0000}

1500.4200

Pads with zeroes.用0填充

#

Digit placeholder

数字占位符

{0:(#).##}

(1500).42

 

.

Decimal point

小数点

{0:0.0}

1500.4

 

,

Thousand separator千位分隔符

{0:0,0}

1,500

Must be between two zeroes.

,.

Number scaling

数字缩放

{0:0,.}

2

Comma adjacent to Period scales by 1000.

%

Percent

百分数

{0:0%}

150042%

Multiplies by 100, adds % sign.

e

Exponent laceholder指数占位符

{0:00e+0}

15e+2

Many exponent formats available.许多指数格式可用

;

Group separator

群分隔符

see below

见下

 

especially useful for formatting currency

 

日期 

字符

类型

示例(System.DateTime.Now)

d

Short date

短日期

10/12/2012

D

Long date

长日期

December 10, 2012

t

Short time

短时间

10:11 PM

T

Long time

长时间

10:11:29 PM

f

Full date & time

长日期和短时间

December 10, 2012 10:11 PM

F

Full date & time (long)

长日期和长时间

December 10, 2012 10:11:29 PM

g

Default date & time

短日期和短时间

10/12/2012 10:11 PM

G

Default date & time (long)

短日期和长时间

10/12/2012 10:11:29 PM

M

Month day pattern

月日模式

December 10

r

RFC1123 date string

RFC1123日期字符串

Tue, 10 Dec 2012 22:11:29 GMT

s

Sortable date string

短日期字符串

2012-12-10T22:11:29

u

Universal sortable, local time

通用,本地时间

2012-12-10 22:13:50Z

U

Universal sortable, GMT

通用 GMT

December 11, 2012 3:13:50 AM

Y

Year month pattern

年月模式

December, 2012

自定义日期格式:

字符

类型

示例

输出

dd

Day

月中的某一天

{0:dd}

10

ddd

Day name

周中某天的缩写名称

{0:ddd}

Tue

dddd

Full day name

周中某天的完整名称

{0:dddd}

Tuesday

f, ff, ...

Second fractions

秒的小数精度

{0:fff}

932

gg, ...

Era

时期或纪元

{0:gg}

A.D.

hh

2 digit hour

12小时制的小时,2位数

{0:hh}

10

HH

2 digit hour, 24hr format

24 小时制的小时,2位数

{0:HH}

22

mm

Minute 00-59

分钟,2位数

{0:mm}

38

MM

Month 01-12

月份数字,2位数

{0:MM}

12

MMM

Month abbreviation

月份的缩写名称

{0:MMM}

Dec

MMMM

Full month name

月份的完整名称

{0:MMMM}

December

ss

Seconds 00-59

秒,2位数

{0:ss}

46

tt

AM or PM

AM/PM 指示项

{0:tt}

PM

yy

Year, 2 digits

不包含纪元的年份

{0:yy}

02

yyyy

Year

包括纪元的四位数的年份

{0:yyyy}

2012

zz

Timezone offset, 2 digits

时区偏移量,2位数

{0:zz}

-05

zzz

Full timezone offset

完整时区偏移量

{0:zzz}

-05:00

:

Separator

默认时间分隔符

{0:hh:mm:ss}

10:43:20

/

Separator

默认日期分隔符

{0:dd/MM/yyyy}

10/12/2012

 

枚举

字符

类型

g

Default (Flag names if available, otherwise decimal)

f

Flags always

d

Integer always

x

Eight digit hex.

 

其他

String.Format("{0:$#,##0.00;($#,##0.00);Zero}", value);

负数的时候输出括号,0的时候输出Zero

String.Format("{0:$#,##0.00;($#,##0.00);Zero}", 12345.00)

将输出$12,345.00

String.Format("{0:$#,##0.00;($#,##0.00);Zero}", -12345.00)

将输出($12,345.00)

String.Format("{0:$#,##0.00;($#,##0.00);Zero}", 0.00)

输出zero

String.Format("{0:(###) ###-####}", 18005551212)

输出(1800) 555-1212

 

C#中的String.Format介绍的更多相关文章

  1. php示例代码之类似于C#中的String.Format方法

    php示例代码之类似于C#中的String.Format方法 原文来自于  http://stackoverflow.com/questions/1241177/c-string-format-equ ...

  2. java中的String.format使用

         format(String  format, Objece...  argues)函数相当于C语言中的printf函数,但是相对来说更灵活.      和C中的printf函数差不多,在fo ...

  3. String.format介绍

    java类中提供的一种方法:String.format(String format, Object ... args) 提供字符串格式化功能: 不同转换符实现不同数据类型到字符串的转换: %s--字符 ...

  4. C#中的String.Format方法(转)

    一.定义String.Format是将指定的 String类型的数据中的每个格式项替换为相应对象的值的文本等效项. 如: (1)string p1 = "Jackie";strin ...

  5. C#中的String.Format方法

    定义String.Format是将指定的String类型的数据中的每个格式项替换为相应对象的值的文本等效项.(1)string p1="xiaomeng";string p2=&q ...

  6. Jquery 中使用String.Format

    第一种方法: String.format = function() { if (arguments.length == 0) return null; var str = arguments[0]; ...

  7. js中的string.format

    String.prototype.format = function(args) { var result = this; if (arguments.length > 0) { if (arg ...

  8. Javascript中的String.format方法实现

    <script type='text/javascript'> String.format = function() { var s = arguments[0]; for (var i ...

  9. Java 实现C#中的String.format效果 解决("我是{0},今年了","whaozl") bug

        /**      * 需要引入com.alibaba.fastjson.1.2.8      * 两种调用方式      * String template1="我是{0},今年{1 ...

随机推荐

  1. Qt没有被正确安装,请运行make install问题的解决

    在网上直接下载别人编译好的Qt库,为自己使用省了不少事.但往往也会遇到些问题,其中Qt version is not properly installed,please run make instal ...

  2. HDMI ARC功能详解及应用介绍

    http://www.icpcw.com/Parts/Peripheral/Skill/3260/326044_2.htm [电脑报在线]很多用户和读者购买了电视以后,都发现自己电视的HDMI接口上经 ...

  3. POJ 1775 Sum of Factorials (ZOJ 2358)

    http://poj.org/problem?id=1775 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1334 题目大意: ...

  4. 魔兽争霸war3心得体会(二):狗转蜘蛛,DK光环+游侠二发

    最近几周,勤奋地在QQ对战平台上,练习war3对战. 10年玩到14年初, 也玩了很多,主要是抱着"随便玩玩"的心态,结果也很显然,可以轻松打赢中等电脑,以及AI不够高的发狂的Hu ...

  5. 安装Centos时“sda必须有一个GPT磁盘标签”

    http://jingyan.baidu.com/article/c45ad29c272326051753e2d1.html

  6. URL validation failed. The error could have been caused through the use of the browser&#39;s navigation

    URL validation failed. The error could have been caused through the use of the browser's navigation ...

  7. Android怎样捕获应用的crash信息

    转载请注明出处:http://blog.csdn.net/fishle123/article/details/50823358 我们的应用不可避免的会发生crash,假设是在调试阶段,我们能够使用Lo ...

  8. 【42.07%】【codeforces 558A】Lala Land and Apple Trees

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. [Angular Directive] Combine HostBinding with Services in Angular 2 Directives

    You can change behaviors of element and @Component properties based on services using @HostBinding i ...

  10. xp2p系统的10点技术创新和经验总结

    最近在开发完善九天鸟的xp2p系统,解决了很多技术问题,特此总结下. 第一个项目开发,非常重要,它对建立开发规范.团队协作.开发效率,有很重大的意义. 1.分页前台AJAX异步分页,用咱们自己的fup ...