How to apply different formatting depending on whether number is positive or negative
I'm outputting a Double
that can be either (+) or negative (-). If the number is a negative the symbol (-) is included automatically, is there a way to do this for positive numbers as well?
The only (horrible) way I can do this is :
If MyNumber <= 0 then
string.Format("{0:0.00}", MyNum开发者_如何学运维ber)
Else
string.Format("+{0:0.00}", MyNumber)
End If
You can use the section separator in your format:
string.Format("{0:+0.00;-0.00}", num);
The format before the semi-colon will be used for positive numbers. The format after will be used for negative numbers. If you want a separate format for zero, add another format after the negative number format:
string.Format("{0:+0.00;-0.00;0.00}", num);
string.Format("{0:+0.00;-0.00;0.00}”,MyNunber);
精彩评论