How to force positive sign (+) on numeric values

  • Thread starter Thread starter Ken Durden
  • Start date Start date
K

Ken Durden

Is it possible to force positive values to have the + sign prefixed on
them?

double f1 = 1024.2;
double f2 = -1024.2;

string.Format( "{0:F}", f1 ); // +1024.2
string.Format( "{0:F}", f2 ); // -1024.2

I was hoping for something either in the string.Format prefix, or
something in the NumberFormatInfo object, but I couldn't seem to find
it.

string.Format( "+{0:F}", f1 );

isn't really an option, because I'm trying to make a grid control
display floating points in this way, and I'm not dynamically
generating the text for each cell.

Thanks,
-ken
 
Hum, could you put your double into a custom class. Then for the ToString()
procedure produce the layout you want?

Class signed_double
{
double m_value;
signed_double(double v) { value = v; }
string ToString() { if (f > 0) then return string.Format{"+{0:F}",
m_value)
else return string.Format{"{0:F}", m_value);
}

I had thought the ToString() function was called by default on the grids.
Not sure about any of this. Just an idea.
 
Ken said:
Is it possible to force positive values to have the + sign prefixed on
them?

double f1 = 1024.2;
double f2 = -1024.2;

string.Format( "{0:F}", f1 ); // +1024.2
string.Format( "{0:F}", f2 ); // -1024.2

I was hoping for something either in the string.Format prefix, or
something in the NumberFormatInfo object, but I couldn't seem to find
it.

string.Format( "+{0:F}", f1 );

isn't really an option, because I'm trying to make a grid control
display floating points in this way, and I'm not dynamically
generating the text for each cell.

It took some careful looking at the docs, but I think I found you an
answer.

Using the numeric custom formatting characters, you can specify separate
formats for positive, negative, and zero values in a single format
string by separating them with the semi-colon character:


double f1 = 1024.2;
double f2 = -1024.2;
double f3 = 0.0;

string fmt = "{0:+0.0##;-0.0##;0.0}";

Console.WriteLine( string.Format( fmt, f1 )); // +1024.2
Console.WriteLine( string.Format( fmt, f2 )); // -1024.2
Console.WriteLine( string.Format( fmt, f3 )); // 0.0

You'll possibly want to use the '0' and '#' formatting characters
differently, depending on how many digits you want to force in the
output. If you don't specify a zero format explicitly, it'll use the
positive number format spec (in case that's what you want).

I must say that I prefer the printf() style of forcing a '+' sign in the
output, but this method does have the advantage of being more flexible
(all of the MSDN samples use it to have negative numbers displayed
inside parentheses).
 
From the custom numeric format strings section of the docs:

"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:
a.. One section: The format string applies to all values.
b.. 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.
c.. 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. "
So using two sections, like so will work:
double f1 = 1024.2;
double f2 = -1024.2;
string.Format( "{0:+0.#####;-0.#####}", f1 ); // +1024.2
string.Format( "{0:+0.#####;-0.#####}", f2 ); // -1024.2
 
Back
Top