jesse jackel
jesse jackel

Reputation: 1

Formatting output in console application

I am having trouble formatting a string in:

Console.WriteLine("SSN: {0}   Gross: {1}   Tax: {2}", t[x].SSN, t[x].Gross.ToString("C"), t[x].Tax.ToString("C"));

It should print:

SSN            Gross         Tax
123456789    $30,000.00     $8400.00

The full code is below:

using System;

namespace TaxPayerDemo
{
    class Program
    {
        static void Main()
        {
            TaxPayer[] t = new TaxPayer[10];
            int x;

            for(x = 0; x < t.Length; ++x)
            {
                // Stores SSN 
                t[x] = new TaxPayer();
                Console.Write("Please enter your SSN >> ");
                t[x].SSN = Console.ReadLine();

                // Stores Gross Income
                Console.Write("Please enter your income >> ");
                t[x].Gross = Convert.ToDouble(Console.ReadLine());
            }

            for (x = 0; x < t.Length; ++x)
            {
                t[x].calcTax();
                Console.WriteLine();
                Console.WriteLine("SSN: {0}   Gross: {1}   Tax: {2}", t[x].SSN, t[x].Gross.ToString("C"),
                    t[x].Tax.ToString("C"));
                         }
            Console.ReadKey();
        }
    }

    class TaxPayer
    {
        private const double LOW_TAXRATE = 0.15;
        private const double HIGH_TAXRATE = 0.28;

        public double Tax { get; set; }
        public double Gross { get; set; }
        public string SSN { get; set; }

        public void calcTax()
        {
            if (Gross < 30000)
            {
                Tax = LOW_TAXRATE * Gross;
            }
            if (Gross >= 30000)
            {
                Tax = HIGH_TAXRATE * Gross;
            }
        }
    }
}

Upvotes: 0

Views: 21690

Answers (3)

John Alexiou
John Alexiou

Reputation: 29274

I would use the same format string for each line (including headers). To get a result like this:

SSN Number   Gross Income  Taxes
172-00-1234  $30,000.00    $8,400.00
137-00-7263  $38,000.00    $8,800.00
138-00-8271  $27,000.00    $7,300.00

Use code like this:

public struct TaxPayer
{
    public string SSN { get; set; }
    public decimal Gross { get; set; }
    public decimal Tax { get; set; }        

}
class Program
{
    static void Main(string[] args)
    {
        var list=new List<TaxPayer>() { 
            new Person() { SSN="172-00-1234", Gross=30000m, Tax=8400m },
            new Person() { SSN="137-00-7263", Gross=38000m, Tax=8800m }, 
            new Person() { SSN="138-00-8271", Gross=27000m, Tax=7300m }, 
        };
        //each number after the comma is the column space to leave
        //negative means left aligned, and positive is right aligned
        string format="{0,-12} {1,-13} {2,-13}";
        string[] heading = new string[] { "SSN Number", "Gross Income", "Taxes" };

        Console.WriteLine(string.Format(format, heading));
        for (int i = 0; i < list.Count; i++)
        {
            string[] row=new string[] { list[i].SSN, list[i].Gross.ToString("C"), list[i].Tax.ToString("C") };
            Console.WriteLine(string.Format(format, row));
        }
    }
}

Of course I would consider moving all formatting code inside TaxPayer as possible with static method to produce a header, and each instance calling .ToString()

public struct TaxPayer
{
    public string SSN { get; set; }
    public decimal Gross { get; set; }
    public decimal Tax { get; set; }

    public static string Formatting="{0,-12} {1,-13} {2,-13}";

    public static string Heading { get { return string.Format(Formatting, new string[] { "SSN Number", "Gross Income", "Taxes" }); } }
    public override string ToString()
    {
        return string.Format(Formatting,
            new string[] { SSN, Gross.ToString("C"), Tax.ToString("C") });
    }
}

and

    Console.WriteLine(TaxPayer.Heading);
    for (int i = 0; i < list.Count; i++)
    {
        Console.WriteLine(list[i]);
    }

Upvotes: 1

Fernando Matsumoto
Fernando Matsumoto

Reputation: 2727

Have a look at Composite Formatting for information about how string formatting works. In order to align text, use the format {0,min_width}:

Console.WriteLine("SSN            Gross          Tax");
Console.WriteLine("{0,-15}{1,-15}{2,-15}", t[x].SSN, t[x].Gross.ToString("C"), t[x].Tax.ToString("C"));

This will align the values to the headers. Feel free to change 15 to any other value; just remember to add the correct spacing in the header (between 'SSN', 'Gross' and 'Tax').

Note that I've used a minimum width of -15. The - means that the text will be left aligned. Use a positive number instead to make it right aligned.

Upvotes: 7

M.kazem Akhgary
M.kazem Akhgary

Reputation: 19189

For Enter put \n and instead of spaces put \t.

Console.WriteLine("SSN:\t\tGross:\t\tTax:\t\t\n{0}\t\t{1}\t\t{2}", t[x].SSN, t[x].Gross.ToString("C"),
        t[x].Tax.ToString("C"));

I used two tabs(\t\t) so its more clear.

Upvotes: 0

Related Questions