【发布时间】:2016-05-21 16:25:45
【问题描述】:
我已经实现了一个价格文本框,其中显示 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")]);
上面这行代码将从数据库中获取数据。
【问题讨论】:
-
TryParse 需要设置国际化参数,这样你就可以强制 .和 , (不同的语言交换它们)
-
你能帮我吗先生@StianSkjelstad
-
@StianSkjelstad 我只需要在数据库中显示逗号。这样当我从数据库中获取数据时,它将显示正确的格式。 12,345
-
stackoverflow.com/questions/23131414/… 这个展示了 Decimal.TryParse() 的一些示例。相同的逻辑适用于大部分 TryParse
标签: asp.net textbox type-conversion comma string.format