C# Format Specifiers Help.

  • Thread starter Thread starter Kevin
  • Start date Start date
K

Kevin

I am looking for a way in C# to format my output. I want
to be able to specify a disply width and then have the
variable printed at minimum with that width.

I can accomplish this is C and C++ as follows:

C - printf("%5d", someInt);

C++ - cout << setw(5) << someInt;

What is the C# equivalent? I have been trying:

Console.Write("{0:D5}", someInt); - This however has
leading zeros placed in it.

Thanks for your Help.
 
Kevin,
What is the C# equivalent? I have been trying:

Console.Write("{0:D5}", someInt); - This however has
leading zeros placed in it.

You can do

Console.WriteLine(String.Format("{0:D}", someInt).PadLeft(5));



Mattias
 
Thank you for your reply.

I have also found that the following works:

Console.Write("{0,5:G}", someInt);

Thanks Again.
 
Back
Top