Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# text alignment

Tags:

string

c#

.net

reading some string from a text file and the writing them to a text file there only a small problem and that's with the alignment of the text. The {4} parameter is what needs to be formatted to the right so that they are all vertically aligned.

while (recordIn != null)
{
    fields = recordIn.Split(DELIM);
    emp.accNumber = Convert.ToInt32(fields[0]);
    emp.lastName = fields[1];
    emp.firstName = fields[2];
    emp.funds = Convert.ToDouble(fields[3]);
    double money = Convert.ToDouble(fields[3].ToString());


    if (money < 0)
    {
        Console.WriteLine("{0},{1},{2}, {3, 2}, {4}", emp.accNumber, emp.lastName, emp.firstName, emp.funds.ToString("F2"), creditOutput);
    }
    else
    {
        Console.WriteLine("{0},{1},{2}, {3, 2} {4}", emp.accNumber, emp.lastName, emp.firstName, emp.funds.ToString("F2"), debitOutput);            
    }
    recordIn = reader.ReadLine();
}
like image 366
MeggMercer Avatar asked Aug 12 '26 04:08

MeggMercer


1 Answers

You can try string.PadLeft or string.PadRight

Also you can do it like this:

To align string to the left (spaces on the right) use formatting patern with comma (,) followed by a negative number of characters: String.Format("{0,–10}", text). To right alignment use a positive number: {0,10}.

like image 172
Hossein Narimani Rad Avatar answered Aug 14 '26 19:08

Hossein Narimani Rad