views:

42

answers:

2

Hi, I'm using C# and I have many integers.

I was wondering if there was a numeric format string that would display the following:

12

as

+12

This is needed as the application I'm working with is mathematical, and when attempting to display something like x + y when x = 2 and y can be either -2 or say for instance 3. I don't want to be displaying:

2 + -2 and 2 + 3

if you understand what I am getting at.

Thanks in advance.

+3  A: 

I don't believe there's a standard format string that does that, but you could use:

x.ToString("+#;-#;+0")

That will give "+1234", "-1234" and "+0" for values 1234, -1234 and 0 respectively. I think that's what you wanted.

Jon Skeet
Works like a dream. Thanks a lot Jon, I knew there'd be an easier way!
ThePower
A: 

You can display 12 as +12 with the following formatting:

string.Format("{0:+0;-0}", 12);
openshac