Use the same separation by semi-column. Read more about that separator at msdn (supports up to three sections)
Console.WriteLine("{0:positive;negative;zero}", +1); //prints positive
Console.WriteLine("{0:positive;negative;zero}", -1); //prints negative
Console.WriteLine("{0:positive;negative;zero}", -0); //prints zero
You can use ToString
on numeric value and pass format there
string formatted = 1.ToString("positive;negative;zero"); //will return "positive"
or use string.Format
as shown in the comment section. But still you need to pass order position {0}
to it.
string formatted = string.Format("{0:positive;negative;zero}", 1);
In order to check for null, you can use null coalescing operator (cast to object is required, since there is no implicit cast from int?
to string
). It becomes quite messy, so I would recommend to consider simple if statement.
int? v = null;
var formatted = string.Format("{0:positive;negative;zero}", (object) v ?? "null");