2015-07-05 34 views
-2

我有我的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

只是圍繞它Math.Round like;你

Math.Round(TotalPrice, 2) // 123456.12 

也可以使用Math.Round(Double, Int32, MidpointRounding) overload來指定MidpointRoundingAwayFromZeroToEvenToEven是默認選項。

+0

我怎樣才能實現的代碼,如果它是基於用戶輸入? txtTotalPrice.Text = Convert.ToString(TotalPrice); –

+0

@PaoloDuhaylungsod只能用作Math.Round(TotalPrice,2)'? –

+0

工作。謝啦! –

0

當您想要變換string時,最好的方法是使用CurrencyFormat。您可以使用下面的代碼:

txtTotalPrice.Text = TotalPrice.ToString("C"); 
txtTotalProducts.Text = TotalProducts.ToString("C"); 

而是其中:

txtTotalPrice.Text = Convert.ToString(TotalPrice); 
txtTotalProducts.Text = Convert.ToString(TotalProducts);