格式化控制台应用程序中的输出

时间:2015-10-18 04:10:09

标签: c#

我在格式化字符串时遇到问题:

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

应该打印:

SSN            Gross         Tax
123456789    $30,000.00     $8400.00

完整代码如下:

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

3 个答案:

答案 0 :(得分:2)

有关字符串格式化工作原理的信息,请查看Composite Formatting。要对齐文字,请使用格式{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"));

这会将值与标题对齐。随意将15更改为任何其他值;只记得在标题中添加正确的间距(在'SSN','Gross'和'Tax'之间)。

请注意,我使用的最小宽度为-15。 -表示文本将保持对齐。请使用正数来使其正确对齐。

答案 1 :(得分:1)

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

  SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Sales where InvoiceDate BETWEEN  '" + S_reportFromDate.Text + "' AND '" + S_ReportToDate.Text + "'", con);
  DataTable t = new DataTable();
  sda.Fill(t);
  ByDateReport_Grid.DataSource = t;

Use code 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

Of course I would consider moving all formatting code inside 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 right aligned, and positive is left 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)); } } } as possible with TaxPayer method to produce a header, and each instance calling static

.ToString()

and

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

答案 2 :(得分:0)

对于Enter put \n而不是空格\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"));

我使用了两个标签(\t\t),因此更清晰。