Outputting tabular data to the console

How to display data in tabular form to console

Page 1 of 1

2 Replies - 7707 Views - Last Post: 18 September 2009 - 05:44 PM Rate Topic: -----

#1 Charlie IronGleet   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Outputting tabular data to the console

Posted 18 September 2009 - 03:20 PM

Could someone please help me fix up the following code? The code logically runs fine -- outputs decimal, hex, octal, binary equivalents for 1-256 -- the problem is that I can't figure out how to get the data to appear in nice even orderly rows and columns --- everything appears skewed.

I'm sure you'll see what I mean right away..

class DecimalConversion
	{
		static void Main(string[] args)
		{
			Console.WriteLine("Decimal  Binary   Octal	Hexdecimal");
			for (int i = 1; i <= 256; i++)
			{

				string x = Convert.ToString(i, 2);
				string y = Convert.ToString(i, 8);
				string z = Convert.ToString(i, 16);
				
				Console.WriteLine("{0}		 {1}		 {2}		 {3}", i, x, y, z);
			}
		}
	}



Thanks alot for your help!
Irongleet

Is This A Good Question/Topic? 0
  • +

Replies To: Outputting tabular data to the console

#2 MentalFloss   User is offline

  • .
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,590
  • Joined: 02-September 09

Re: Outputting tabular data to the console

Posted 18 September 2009 - 03:31 PM

Format it in the writeline.

Something like this, but may require some tweeking:

class DecimalConversion
{
	static void Main(string[] args)
	{
		Console.WriteLine("Decimal  Binary   Octal	Hexdecimal");
		for (int i = 1; i <= 256; i++)
		{

			string x = Convert.ToString(i, 2);
			string y = Convert.ToString(i, 8);
			string z = Convert.ToString(i, 16);

			Console.WriteLine("{0,-10}{1,-10}{2,-10}{3,-10}", i, x, y, z);
		}

		Console.ReadLine();
	}
}



Edit: Update code...

Posted Image

class DecimalConversion
{
	static void Main(string[] args)
	{
		Console.WriteLine("{0,-15}{1,-15}{2,-15}{3,-15}", "Decimal", "Binary", "Octal", "Hexadecimal");
		for (int i = 1; i <= 256; i++)
		{

			string x = Convert.ToString(i, 2);
			string y = Convert.ToString(i, 8);
			string z = Convert.ToString(i, 16);

			Console.WriteLine("{0,-15}{1,-15}{2,-15}{3,-15}", i, x, y, z);
		}

		Console.ReadLine();
	}
}


This post has been edited by MentalFloss: 18 September 2009 - 03:39 PM

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Outputting tabular data to the console

Posted 18 September 2009 - 05:44 PM

My saved links on .NET formatting:
http://blog.stevex.n...ting-in-csharp/
and
http://blogs.msdn.co.../29/564426.aspx
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1