开发者

Formatting Double in C#

开发者 https://www.devze.com 2023-03-07 03:40 出处:网络
I want to to format a double values like ###,###.00 and I\'m trying the following code: double num = 0.00;

I want to to format a double values like ###,###.00 and I'm trying the following code:

double num = 0.00;
Console.WriteLine("Value with two decimal places: " + 
                      "{0:###,###.00}", num); 

It works fine for all values except 0 or 0.00 etc. for 0 it outputs .00 whereas I want it to emit 0.00开发者_JAVA百科.


Then you should use this:

double num = 0.00;
Console.WriteLine("Value with two decimal places: " + 
                      "{0:###,##0.00}", num); 

0 means: Always print a digit at this place, whether it exits or not. Prints 0 if it doesn't exist.
# means: Only print a digit at this place, if it really exists.

0

精彩评论

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