消息框格式

Message box formatting

我需要我的总计采用货币格式。我的代码不允许我将长字符串输出到消息框中。下面是我试图输出到消息框中的内容。我如何格式化我的总数以显示为货币。

        MessageBox.Show(cmboCrust.GetItemText(cmboCrust.SelectedItem) + " - " + cmboSize.GetItemText(cmboSize.SelectedItem)
                     + "\nSauce: " + cmboSauce.GetItemText(cmboSauce.SelectedItem) +
                    "\nToppings (.50 each): " + topings +
                    "\n\nPizza total: {0:C}" + pizzaTotal +
                    "\n\nDrink selection: " +
                    "\n\t" + sodaTotal + " soda(s)" +
                    "\n\t{0:C}" + waterTotal + " water(s)" +
                    "\nDrink Total: {0:C}" + drinksTotal +
                    "\n\nSpecialty Items: " + specialtyMessage +
                    "\nAmount Due: {0:C}" + billTotal +
                    "\n\n Deliver to: " + txtBxName.Text + ", " + txtBxAddress.Text
                     , "D & G Pizza Express Order");


        string output = string.Format(cmboCrust.GetItemText(cmboCrust.SelectedItem), " - ", cmboSize.GetItemText(cmboSize.SelectedItem),
                     "\nSauce: ", cmboSauce.GetItemText(cmboSauce.SelectedItem),
                    "\nToppings (.50 each): ", topings,
                    "\n\nPizza total: {0:C}", pizzaTotal,
                    "\n\nDrink selection: ",
                    "\n\t", sodaTotal, " soda(s)",
                    "\n\t{0:C}", waterTotal, " water(s)",
                    "\nDrink Total: {0:C}", drinksTotal,
                    "\n\nSpecialty Items: ", specialtyMessage,
                    "\nAmount Due: {0:C}", billTotal,
                    "\n\n Deliver to: ", txtBxName.Text, ", ", txtBxAddress.Text);

以下示例显示带有两位小数的货币符号。

billTotal.ToString("C");

嗯,你可以这样做..

            MessageBox.Show(
            String.Format(
                "\r\nSauce: {0} \r\nToppings (.50 each): {1} \r\nPizza total: {2:C} \r\nDrinkSection", 1, 2, 3));

你明白了..只需用你的变量替换 1,2,3

编辑。 String.Format 增加可读性。

billTotal.ToString("C", CultureInfo.CurrentCulture);
billTotal.ToString("C", new CultureInfo("en-US");

您可以使用重载 ToString() 方法根据当前区域性或所需区域性设置格式。