问题
i have implemented a price text box wherein it shows 12,345 instead of 12345. Or sometimes. 12,345.00. However when being submitted to the database, it shows 12345. the comma is gone. any tricks on this? here is my place order button code:
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()));
Any tricks that i can do on the total price?
I tried using string.format to try if it will get 12,345 but i failed. here is the code:
lblTotalProducts.Text = Convert.ToString(dtCustomerDetails.Rows[0][String.Format("{0:#,000.00}","TotalProducts")]);
this line of code above will get data from the database.
回答1:
In numeric data storage, the comma thousand separator is insignificant. It is only a presentation artifact. In your case, let the database store the value as 12345
, but display it as 12,345.00
using string.Format
method. Please see Standard Numeric Formats.
edit: lblTotalProducts.Text = String.Format("{0:#,000.00}",dtCustomerDetails.Rows[0]["TotalProducts"]);
来源:https://stackoverflow.com/questions/37359906/comma-on-price-value-is-not-being-read