How can I align text in columns using Console.WriteLine?

前端 未结 8 1179
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 22:11

I have a sort of column display, but the end two column\'s seem to not be aligning correctly. This is the code I have at the moment:

Console.WriteLine(\"Cust         


        
8条回答
  •  有刺的猬
    2020-11-28 22:50

    Instead of trying to manually align the text into columns with arbitrary strings of spaces, you should embed actual tabs (the \t escape sequence) into each output string:

    Console.WriteLine("Customer name" + "\t"
        + "sales" + "\t" 
        + "fee to be paid" + "\t" 
        + "70% value" + "\t" 
        + "30% value");
    for (int DisplayPos = 0; DisplayPos < LineNum; DisplayPos++)
    {
        seventy_percent_value = ((fee_payable[DisplayPos] / 10.0) * 7);
        thirty_percent_value = ((fee_payable[DisplayPos] / 10.0) * 3);          
        Console.WriteLine(customer[DisplayPos] + "\t" 
            + sales_figures[DisplayPos] + "\t" 
            + fee_payable + "\t\t"
            + seventy_percent_value + "\t\t" 
            + thirty_percent_value);
    }
    

提交回复
热议问题