'+' sign in non-negative numeric

  • Thread starter Thread starter Peter Luschny
  • Start date Start date
P

Peter Luschny

Hi all,

is there an equivalent to C++ 'showpos' for C# ?
This format generates a '+' sign in non-negative
generated numeric output.

Thanks, Peter
 
Peter,

You can create an instance of the NumberFormatInfo class, and set the
PositiveSign property to "+". When you use this format info (or another
with this property modified), it should print out positive numbers with a
plus.

You can probably modify the NumberFormatInfo instance returned from the
static NumberFormat property on the CultureInfo class.

Hope this helps.
 
Hi Peter,

There is an excerpt form MSDN form the section "Custom Numeric Format
Strings"
ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconcustomnumericformatstrin
gs.htm

"Different formatting can be applied to a string based on whether the value
is positive, negative, or zero. To produce this behavior, a custom format
string can contain up to three sections separated by semicolons:

- One section: The format string applies to all values.

- Two sections: The first section applies to positive values and zeros, and
the second section applies to negative values. If the number to be formatted
is negative, but becomes zero after rounding according to the format in the
second section, then the resulting zero is formatted according to the first
section.

- Three sections: The first section applies to positive values, the second
section applies to negative values, and the third section applies to zeros.
The second section might be left empty (by having nothing between the
semicolons), in which case the first section applies to all nonzero values.
If the number to be formatted is nonzero, but becomes zero after rounding
according to the format in the first or second section, then the resulting
zero is formatted according to the third section."

that means you can do the following formatting

int i = 100
int j = -100

Console.WriteLine("i = {0}", i.ToString("+#;-#"));
Console.WriteLine("j = {0}", j.ToString("+#;-#"));

the result is
i = +100
j = -100
 
Nicholas Paldino said:
You can create an instance of the NumberFormatInfo class, and set the
PositiveSign property to "+". When you use this format info (or another
with this property modified), it should print out positive numbers with a
plus.

Hmm.., well let's have a look at the following fragment:

NumberFormatInfo nfi = new NumberFormatInfo();
nfi.PositiveSign = "#";

double d = Double.Parse(" #12.34", NumberStyles.Any, nfi);
Console.WriteLine("d = {0:F}", d);

string w = d.ToString(nfi);
Console.WriteLine(w);

If I understand you right, you expect the parsing ok
and the output to be #12.34. This would be fine for me,
but it is /not/ the case. The parsing is indeed ok,
but the output is 12.34.

And this behaviour is what the documentation announces:

PositiveSign. Hinweise: Diese Eigenschaft wird nur für
das Analysieren numerischer Zeichenfolgen, nicht für
deren Formatierung verwendet. (German ;-)

So I have not yet found the solution (besides doing
the formatting by myself, of course).

Thanks. Peter
 
Stoitcho Goutsev said:
int i = 100
int j = -100

Console.WriteLine("i = {0}", i.ToString("+#;-#"));
Console.WriteLine("j = {0}", j.ToString("+#;-#"));

the result is
i = +100
j = -100

Ah, yes, this works. Thank you!
Peter
 
Back
Top