0

I have a DisplayResults method and when it's called it outputs its contents to the console. I'm having difficulty getting it to align well. I have used tabs \t to align it but it could look better. Here is the code:

        public static void DisplayResults(string empName, decimal empWeeklySales, decimal COMMISSION_RATE, decimal grossPay, 
                                      decimal INCOME_TAX_RATE, decimal incomeTaxPayable, decimal PRSI_RATE, decimal prsiPayable,
                                      decimal RETIREMENT_CONTRIBUTION_RATE, decimal retirementContribution, 
                                      decimal totalDeductions, decimal takeHomePay)
    {
        Console.WriteLine("\n*************** WEEKLY PAYROLL APP ***************\n");

        Console.WriteLine("Employee Name: \t\t\t\t{0}\n", empName);
        Console.WriteLine("This week's sales: \t\t\t{0:n}", empWeeklySales);
        Console.WriteLine("Commission Rate: \t{0:F2}%\n", COMMISSION_RATE);

        Console.WriteLine("Gross Pay: \t\t\t\t{0:F2}", grossPay);
        Console.WriteLine("Income Tax: \t\t({0:F2}%) \t{1:F2}", INCOME_TAX_RATE, incomeTaxPayable);
        Console.WriteLine("PRSI: \t\t\t({0:F2}%) \t{1:F2}", PRSI_RATE, prsiPayable);
        Console.WriteLine("Retirement Contribution: ({0:F2}%) \t{1:F2}", RETIREMENT_CONTRIBUTION_RATE, retirementContribution);

        Console.WriteLine("Total Deductions: \t\t\t{0:F2}\n", totalDeductions);

        Console.WriteLine("Take Home Pay: \t\t\t\t{0:F2}\n", takeHomePay);

        Console.WriteLine("\n**************************************************");
    }

When I output it, it looks like this: What appears in the console window

As you can see, it doesn't line up very well. I would like the percent values in the middle and the money values to the right, to both be right-aligned. So, ideally the text would all be left aligned, the middle area with the percent values right-aligned and an outer area of money values to the right to be right-aligned. I hope this makes sense. Can anyone advise me on how best to achieve this? Thank you.

NepSyn14
  • 163
  • 2
  • 11
  • You could make a WinForms application. The console is where you don't actually put any effort into the UI. – SimpleVar Oct 25 '15 at 21:19

1 Answers1

6

You could use string formatting with alignment to create columns of characters. This is feasible when your output console uses a fixed width font.

For example your first line could be written as

Console.WriteLine("{0,-40}{1,-50}", "Employee Name:", empName);

This will write "Employee Name;" in a column of 40 characters aligned on the left side and filling the remainder with spaces to reach the 40 characters width. Then a new column with a 50 chars width will receive the value of empname aligned on the left of the colum

The line with the Income tax rate could be written as

Console.WriteLine("{0,-30}{1,10:F2}%{2,50:F2}", 
              "Income Tax:", INCOME_TAX_RATE, incomeTaxPayable);

this will reduce the first column to just 30 chars for the label and add a column with 10 character that will be filled with the value of INCOME_TAX_RATE aligned on the right. Of course this could be repeated for the other lines

This is well explained in the MSDN article called Composite Formatting

Steve
  • 213,761
  • 22
  • 232
  • 286
  • Thank you! That helps a lot. I was using the {0, 10} initially, but because I was also using :F2, I somehow thought the value 10 went after it i.e. {0:F2, 10}. As you can imagine this didn't work. I didn't know it should be {0, 10:F2} Thank you for your help. :) – NepSyn14 Oct 25 '15 at 21:37