开发者

How to apply different formatting depending on whether number is positive or negative

开发者 https://www.devze.com 2023-03-30 23:37 出处:网络
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?

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);
0

精彩评论

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