价格值的逗号未被阅读

时间:2016-05-21 06:59:06

标签: asp.net textbox type-conversion comma string.format

我已经实现了一个价格文本框,其中显示12,345而不是12345.或者有时。 12,345.00。但是当提交到数据库时,它显示12345.逗号消失了。对此有什么诡计?这是我的下订单按钮代码:

 protected void btnPlaceOrder_Click(object sender, EventArgs e)
    {
        string productids = string.Empty;
        DataTable dt;
        if (Session["MyCart"] != null)
        {
            dt = (DataTable)Session["MyCart"];

            decimal totalPrice, totalProducts;
            bool totalPriceConversionResult = decimal.TryParse(txtTotalPrice.Text, out totalPrice), totalProductsConversionResult = decimal.TryParse(txtTotalProducts.Text, out totalProducts);


            ShoppingCart k = new ShoppingCart()
            {
                CustomerName = txtCustomerName.Text,
                CustomerEmailID = txtCustomerEmailID.Text,
                CustomerAddress = txtCustomerAddress.Text,
                CustomerPhoneNo = txtCustomerPhoneNo.Text,
                TotalProducts = totalProductsConversionResult ? Convert.ToInt32(totalProducts) : 0,
                TotalPrice = totalPriceConversionResult ? Convert.ToInt32(totalPrice) : 0,
                ProductList = productids,
                PaymentMethod = rblPaymentMethod.SelectedItem.Text

            };
            DataTable dtResult = k.SaveCustomerDetails();

            for (int i = 0; i < dt.Rows.Count; i++) // loop on how many products are added by the user
            {
                ShoppingCart SaveProducts = new ShoppingCart()
                {
                    CustomerID = Convert.ToInt32(dtResult.Rows[0][0]),
                    ProductID = Convert.ToInt32(dt.Rows[i]["ProductID"]),
                    TotalProducts = Convert.ToInt32(dt.Rows[i]["ProductQuantity"]),
                };
                SaveProducts.SaveCustomerProducts();
            }
            var customerId = Convert.ToInt32(dtResult.Rows[0][0]);
            Response.Redirect(String.Format("OrderSummary.aspx?Id={0}", customerId.ToString()));

我可以对总价格做什么招数?

我尝试使用string.format来尝试它是否会获得12,345但我失败了。这是代码:

lblTotalProducts.Text = Convert.ToString(dtCustomerDetails.Rows[0][String.Format("{0:#,000.00}","TotalProducts")]);

上面这行代码将从数据库中获取数据。

1 个答案:

答案 0 :(得分:0)

在数字数据存储中,逗号千位分隔符无关紧要。它只是一个演示工件。在您的情况下,让数据库将值存储为12345,但使用12,345.00方法将其显示为string.Format。请参阅Standard Numeric Formats

编辑:lblTotalProducts.Text = String.Format("{0:#,000.00}",dtCustomerDetails.Rows[0]["TotalPr‌​oducts"]);