Tuesday, January 20, 2009

Add '+' or '-' before float value using ToString()

Assuming you want the sign of the number to be displayed rather than an arbitrary '+', then try the following:

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

Where 'a' is your variable. This is a format string with sections,separated by semi-colons. The first section (+#.###) applies topositive numbers, the second (-#.###) applies to negative numbers andthe last applies to zero.