开发者

A string.Format question (.NET)

Can someone elaborate on the following format string? I don't completely realize the meaning.

Stri开发者_如何转开发ng.Format("{0:#,0.##}", money);

Thanks.


I don't have my development system with me so I can't verify what I'm about to say, but here's my interpretation:

The format portion is "#,0.##". I'm thinking the "#,0" portion specifies a comma should separate thousands (e.g. 1,000,000). And the ".##" is specifying the number of digits after the decimal. I would have thought you'd need ".00" to force two digits (which would be normal for currency). But I would expect what you have to at least cause rounding to two digits after the decimal.

Did you try it?


it means that the money have thousand separator(,) and if in decimal values it will be rounded to two digits after decimal, and if there is only decimal values(.256) it will be (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

Explanation:

 "{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


I believe it is a currency format with a thousands separator and showing only the first 2 places after the decimal if decimal values are included.

Good overview of custom format strings here:
http://blog.stevex.net/string-formatting-in-csharp/

This link includes more details including a description of a more liberal form of the thousands separator with the "," char:
http://msdn.microsoft.com/en-us/library/0c899ak8%28v=vs.71%29.aspx


Basically, this set the formatting to have a thousands separator and 2 decimal digits. According to Microsoft documentation, numbers will be rounded to the correct decimal places.

This is a good resource for custom numeric format strings : http://msdn.microsoft.com/en-us/library/0c899ak8.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜