我是一个初学者,正在学习 C#。我正在制作一个模拟购物清单收据程序来管理您的购物。我生成了 .txt 收据,但在正确对齐字符串时遇到了问题,以下是我的代码:
public static void GenerateNewReciept(Customer customer)
{
List<ShoppingItems> customerPurchaedItems;
try
{
sw = File.AppendText("receipt.txt");
//Customer object
Customer customers = customer;
//List of all 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);
for (int i = 0; i < 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,-20} {1,25:C2}", item.ItemName, item.Price));
sw.WriteLine(String.Format("{0,-20} {1,25:C2}", "Total", total));
- 在这里,我希望商品的价格可以右对齐,但是一些商品名称较长,当应用了25个空格时,它们会错位。总价也是同样的情况。