How to do Format(anumber, "###") in C#?

  • Thread starter Thread starter GoodMorningSky
  • Start date Start date
G

GoodMorningSky

I want keep 3 character space for any numbers between 0 ~ 999.
[ 0] [999] : all numbers keeps 3 space using Integer.ToString()
How can I do this?
 
Hi GoodMorningSky,

There might be better ways but

int n = 3;
string number = n.ToString().PadLeft(3, ' ');

will get you " 3" and "999".
 
How about this?
Console.WriteLine (String.Format("[{0,3}]", <number>));

I want keep 3 character space for any numbers between 0 ~ 999.
[ 0] [999] : all numbers keeps 3 space using Integer.ToString()
How can I do this?
 
Back
Top