2010-12-13 24 views
2

有人能闡述如下格式的字符串?我沒有完全意識到這個意思。一個問題的String.format(.NET)

String.Format("{0:#,0.##}", money); 

謝謝。

+1

請參閱C#中的字符串格式:http://blog.stevex.net/string-formatting-in-csharp/ – 2010-12-13 04:19:02

回答

2

這意味着資金有千個分隔符(,),如果在小數值將小數後四捨五入到兩位數,如果有僅十進制值(0.256)這將是(0.27)

decimal money=12341257 //output= 12,341,257 
decimal money=1257  //output= 1,257 
decimal money=1257.25  //output= 1,257.25 
decimal money=1257.2468 //output= 1,257.25 
decimal money=.50   //output= 0.50 
decimal money=.759  //output= 0.76 

說明:

"{0:#,0.##}" 
    #,0 //means that , as thousand seperator 
    0.## //means that 0 is placed before if only decimal values as .56 to 0.56 
    0.## //means if contains decimal then only display 2 digits after decimal 
    0.00 //means 2 digits after decimal must be displayed whether or not money contains decimal value 
3

我沒有我的開發系統,我,所以我無法驗證我要告訴你,但這裏是我的解釋:

格式部分是「#,## 0」。我認爲「#,0」部分指定一個逗號應該分隔數千(例如1,000,000)。 「。##」指定小數點後的位數。我原以爲你需要「.00」強制兩位數(這對貨幣來說是正常的)。但我期望你至少要在小數點後舍入到兩位數。

你試過了嗎?