views:

1301

answers:

2

Is there any way I can specify a standard or custom numeric format string to always output the sign, be it +ve or -ve (although what it shoudl do for zero, I'm not sure!)

+12  A: 

Yes, you can. There is conditional formatting. See Conditional formatting in MSDN

eg:

string MyString = number.ToString("+#;#");

Where each section separated by a semicolon represents positive and negative numbers

or:

string MyString = number.ToString("+#;#;0");

if you don't want the zero to have a plus sign.

gcores
Excellent - thanks!
Craig Shearer
+9  A: 

Beware, when using conditional formatting the negative value doesn't automatically get a sign. You need to do

string MyString = number.ToString("+#;-#;0");
Luk