Вопрос

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 
Это было полезно?

Решение

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);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top