C# formatting text (right align) -


i beginner learning c#, making mock shopping list receipt program manage shopping. generate .txt receipt having problem right align string here code;

public static void generatenewreciept(customer customer) {     list<shoppingitems> customerpurchaeditems;     try     {         sw = file.appendtext("receipt.txt");          //customer object         customer customers = customer;         //list of items in list         list<shoppinglist> customeritems = customer.customershoppinglist;         datetime todaydandt = datetime.now;          //making reciept layout         sw.writeline("date generated: " + todaydandt.tostring("g", cultureinfo.createspecificculture("en-us")));         sw.writeline("customer: " + customer.fname + " " + customer.sname1);          (int = 0; < customeritems.count; i++)         {             customerpurchaeditems = customeritems[i].shoppingitems;             foreach (shoppingitems item in customerpurchaeditems)             {                 sw.writeline(string.format("{0,0} {1,25:c2}", item.itemname, item.price));               }         }          sw.writeline("total {0,25:c2}", customeritems[0].computetotalcost());         sw.close();     }     catch (filenotfoundexception)     {         console.writeline("file not found!");     } } 

sw.writeline(string.format("{0,0} {1,25:c2}", item.itemname, item.price)); sw.writeline("total {0,25:c2}", - want prices of items right aligned items have larger names, meaning when 25 spacing applied out of place.this same total.

there is way built-in formatting:

using system;  namespace demo {     static class program     {         public static void main()         {             printformatted("short", 12.34);             printformatted("medium", 1.34);             printformatted("the longest", 1234.34);         }          static void printformatted(string description, double cost)         {             string result = format(description, cost);             console.writeline(">" + result + "<");         }          static string format(string description, double cost)         {             return string.format("{0,15} {1,9:c2}", description, cost);         }     } } 

this prints:

>          short    £12.34< >         medium     £1.34< >    longest £1,234.34< 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -