问题
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}", MyNumber)
Else
string.Format("+{0:0.00}", MyNumber)
End If
回答1:
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);
回答2:
string.Format("{0:+0.00;-0.00;0.00}”,MyNunber);
来源:https://stackoverflow.com/questions/7193917/how-to-apply-different-formatting-depending-on-whether-number-is-positive-or-neg