1

嗨,我在使用这种格式显示金额时遇到问题: 0,000.00 。我正在使用网格视图和数据阅读器来显示 mySQL 数据库中的金额,以 Decimal 作为我的数据类型,我的输出仅显示 0000.00

你能帮我解决这个问题吗?谢谢你。

private void DisplayOrderDetails(int nOrderNo)

        {
            OpenConnection();

            SqlCommand cmdSelect = new SqlCommand();
            cmdSelect.Connection = cn;
            cmdSelect.CommandType = CommandType.Text;
            cmdSelect.Transaction = trnOrder;
            cmdSelect.CommandText =
                "SELECT OrderDetailNo, OrderNo, PackagingOutside, Quantity, Unit, ProductNo, ProductName, " +
                "ProductSize, PackagingInside, " +
                "SellingDiscount1, SellingDiscount2, SellingDiscount3, SellingDiscount4, " +
                "SellingPrice, Amount FROM OrderDetails WHERE OrderNo = '"
                + nOrderNo + "'";

            SqlDataAdapter daDetail = new SqlDataAdapter();
            daDetail.SelectCommand = cmdSelect;

            DataSet ds = new DataSet();
            daDetail.Fill(ds, "OrderDetails");

            grdDetails.DataSource = null;
            grdDetails.DataSource = ds.Tables["OrderDetails"];

            DisplayTotal();
        }

 private void DisplayTotal()

        {
            double dTotal = 0;

            //for encountering errors in the future
            try
            {
                for (int nRow = 0;
                    nRow <= dsDetail.Tables["OrderDetails"].Rows.Count - 1;
                    nRow++)
                {
                    dTotal = dTotal + 
                        Convert.ToDouble(dsDetail.Tables["OrderDetails"].Rows[nRow]["Amount"].ToString());
                }
            }
            catch //(Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }

            lblTotal.Text = String.Format("{0:#,###,##0.00}", dTotal);
        }
4

3 回答 3

2

对于货币或货币格式,您需要使用C

因此将您的代码更改为

 lblTotal.Text = dTotal.ToString("C", CultureInfo.CurrentCulture);

或者

 lblTotal.Text = String.Format("{0:C}", dTotal);

参考

于 2013-10-02T13:51:50.233 回答
1
lblTotal.Text = dTotal.ToString("C");

不要忘记这使用默认的文化信息并显示默认货币。您还可以通过提供一个实例来更改此行为CultureInfo

http://msdn.microsoft.com/en-us/library/shxtf045.aspx

于 2013-10-02T13:54:46.880 回答
1

您可以使用CultureInfo对象并将目标文化传递给String.Format,这将使用正确的文化信息显示货币和其他字符串:

var cultureInfo = System.Globalization.CultureInfo.CreateSpecificCulture("de-DE"); // for example, the German culture info will give you the commas for thousand separators instead of the decimal point
lblTotal.Text = String.Format(cultureInfo, "{0:C}", dTotal);

参考CultureInfo.CreateSpecificCulture: http: //msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.createspecificculture.aspx 上述重载参考:http String.Format: //msdn.microsoft.com/en-us/library /1ksz8yb7.aspx

本部分可能值得从String.Format文档中阅读:http: //msdn.microsoft.com/en-us/library/system.string.format.aspx#Format_Culture

于 2013-10-02T13:57:56.517 回答