我的updatetobill代码中包含此代码。我打算把它四舍五入到最近的第十个我想?例如价格是123456.123456,我想做的是把它作为123456.12因为它的价格,我需要美分。在此先感谢任何帮助:)
private void UpdateTotalBill()
{
double vat = 0;
double TotalPrice = 0;
long TotalProducts = 0;
foreach (DataListItem item in dlCartProducts.Items)
{
Label PriceLabel = item.FindControl("lblPrice") as Label; // get price
TextBox ProductQuantity = item.FindControl("txtProductQuantity") as TextBox; // get quantity
double ProductPrice = Convert.ToInt64(PriceLabel.Text) * Convert.ToInt64(ProductQuantity.Text); //computation fro product price. price * quantity
vat = (TotalPrice + ProductPrice) * 0.12; // computation for total price. total price + product price
TotalPrice = TotalPrice + ProductPrice+40 +vat;
TotalProducts = TotalProducts + Convert.ToInt32(ProductQuantity.Text);
}
Label1.Text = Convert.ToString(vat);
txtTotalPrice.Text = Convert.ToString(TotalPrice); // put both total price and product values and converting them to string
txtTotalProducts.Text = Convert.ToString(TotalProducts);
}
答案 0 :(得分:0)
只需围绕它Math.Round
;
Math.Round(TotalPrice, 2) // 123456.12
您也可以使用Math.Round(Double, Int32, MidpointRounding)
overload指定MidpointRounding
是AwayFromZero
或ToEven
。 ToEven
是默认选项。
答案 1 :(得分:0)
要转换string
时执行此操作的最佳方法是使用CurrencyFormat
。您可以使用以下代码:
txtTotalPrice.Text = TotalPrice.ToString("C");
txtTotalProducts.Text = TotalProducts.ToString("C");
取而代之的是:
txtTotalPrice.Text = Convert.ToString(TotalPrice);
txtTotalProducts.Text = Convert.ToString(TotalProducts);