C#中的String.Format介紹


關鍵字: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

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM