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
--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"Ken Durden" <cr*************@hotmail.com> wrote in message
news:18**************************@posting.google.c om...
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