Custom numeric format string to always display the sign

Craig Shearer picture Craig Shearer · Dec 7, 2008 · Viewed 92.8k times · Source

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 should do for zero, I'm not sure!)

Answer

gcores picture gcores · Dec 7, 2008

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

eg:

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

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.