Numeric formatting question
-
In the following String.Format call it will format a number to 6 zero-padded digits, prepend the plus or minus sign and then pad with a space on the left to 8 characters:
String.Format({0,8:+000000;-000000}, num);
// result is of the form " +002009" What format string can I use to format a number to 7 space-padded digits and prepend the plus or minus sign before the spaces? I am looking for something like"+ 2009"
BethI have added padding support to my ApplyFormat method, so now
2009.ApplyFormat ( "' '+;-''6 : 0 ;0" )
will do that. But now I want to cache the interpretation of the format specifier to streamline things a bit, so I'm not yet ready to update the article.modified on Saturday, February 28, 2009 6:26 PM
-
Will this suit the need? :-D
System.Console.WriteLine ( "{0,1: + ; - }{0,7 : 0 ; 0 }" , 2009 ) ;
System.Console.WriteLine ( "{0,1: + ; - }{0,7 : 0 ; 0 }" , -2009 ) ;(SPACEs added to avoid smileys; remove them.) (Third try.)
modified on Friday, February 27, 2009 1:55 PM
-
The original code was mangled because of fixes required for smileys. Here is the correct code:
System.Console.WriteLine ( "{0 , 1 : + ; -}{0 , 7 : -O ; 0}" , 2009 ) ;
System.Console.WriteLine ( "{0 , 1 : + ; -}{0 , 7 : -O ; 0}" , -2009 ) ;What I posted works fine for me (once the SPACEs are removed).