I am translating some code from VB to C# and came across this:
Format(seg.R, " 00.00000;-00.00000") // <-- There is a leading space in the first format string
...which prints...
00.00000 //or...
-00.00000
...depending on whether the decimal is positive or negative. Is there an easy way to do this with the C# string.Format
or myObj.ToString("[format here]")
functions?
EDIT: Notice the extra space in the format string. This makes the strings the same length by adding a leading space when there is no negative sign.
Yes:
http://blog.stevex.net/string-formatting-in-csharp/
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
Here's a quick answer for what you posted above:
whateverstring.ToString(" 00.00000;-00.00000");
-29.69 -> "-29.69000"
48 -> " 48.00000" <-notice space padding the front.
The semi colon separated formatting string: "00.00;##.##" applies the 00.00 pattern to Positive and zero values, and the ##.## pattern to negative values.
In other words, what you had before for a formatting string works without any tweaking :-)