C# Question

String.Format. Looking whether needed format exist

I need a special string format for numbers. Maybe someone know such variant and can help me.

Algorithm:

1) if number is < 0 (f.e. -1), then I need to have such string: "-1".

2) if number is >= 0 (f.e. 1), then I need to have such string: " 1".

I don't need help like "make with if or ternary operator" - I know how to do that, but I'm interesting in existing format. Thank you.

Answer

The format you are looking for is this one. Since zero is specific scenario, it is added explicitly.

string numberToString = number.ToString(" #;-#;0");