0

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;
            }
        }
    }
}
1
  • Yes but i would like it Where it says SSN, Gross, Tax up top and the 123456789, $30,000.00, $8,400.00 under the the correct names Commented Oct 18, 2015 at 4:23

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Another questions is that i am suppose to have a readonly tax; for the Tax but my text box is unclear on that its pretty vague
You should ask another question if there is another unrelated problem in your code.
Its for this question just for the part under 'Class TaxPayer'
@jessejackel what do you mean by but my text box is unclear on that its pretty vague
@jessejackel Your question is about formatting text. If you want to know something else, then either edit your current question or, better yet, ask another question, since your two problems appear to be unrelated (just because they appear in the same code, it doesn't mean they are related problems; one is to be about formatting, the other seems to be about properties). Also, as mentioned by @M.kazemAkhgary, the meaning of text box in your comment is pretty vague and hard to understand.
|
1

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]);
    }

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.