Home All Groups Group Topic Archive Search About

format number including + or - sign

Author
20 Nov 2007 7:38 PM
Shailesh Patel
Hi,
Is there any formatting that adds + or - sign before number. Normally it
adds - sigh if number is negative and no sign if positive.
I want to add + sign when number is positive.

Thank you in advance.

Shailesh

Author
20 Nov 2007 8:03 PM
Cor Ligthert[MVP]
Shailesh,

The most simple is mostly something as

A = 0 - A

As you mean as a string then use the overloaded ToString with the numeric
formatprovider.

Cor
Author
21 Nov 2007 2:40 AM
Herfried K. Wagner [MVP]
"Cor Ligthert[MVP]" <notmyfirstn***@planet.nl> schrieb:
> The most simple is mostly something as
>
> A = 0 - A

Did you mean 'A = -A', which will cause the sign to be "swapped"?

> As you mean as a string then use the overloaded ToString with the numeric
> formatprovider.

I took a look at the custom numeric format strings documentation and I could
not come up with a formatting pattern which will cause the sign of the
number to be shown regardless if it is a "+" or a "-".  'NumberFormatInfo'
contains properties which will return the sign characters, but documentation
states that they are only used for parsing and not for formatting.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
21 Nov 2007 5:43 AM
Cor Ligthert[MVP]
>>
>> A = 0 - A
>
> Did you mean 'A = -A', which will cause the sign to be "swapped"?
>
Yes, nicer with the same result of course

I have people seen doing this.

A = -1 * A which I don't like at all.

Cor
Author
21 Nov 2007 3:41 AM
Family Tree Mike
I found this one that works.  Never had a need for it, but it does work:

dim x as double
string.Format("{0:+0;-0;0}", x)

I have to credit this url:
http://blogs.msdn.com/kathykam/archive/2006/03/29/564426.aspx


Show quoteHide quote
"Shailesh Patel" wrote:

> Hi,
> Is there any formatting that adds + or - sign before number. Normally it
> adds - sigh if number is negative and no sign if positive.
> I want to add + sign when number is positive.
>
> Thank you in advance.
>
> Shailesh
>
>
>