如何使用标签进行简单的发票计算?

时间:2015-07-09 07:21:38

标签: c# asp.net

我有这个代码作为账单的计算。

space

我想要做的是使用标签显示从后端到前端的计算。例如1000(产品价格)×3(产品数量)+增值税(100)+运费(4)=(总计)4000。像这样的东西?

2 个答案:

答案 0 :(得分:0)

我想你想要这样的东西,

lblResult.Text=lblPrice.Text+" (ProductPrice) * " +lblProductQuantity.Text + " + (Product Quantity) + "+vat+" (VAT) + " + lblDeliveryCharge.Text + " (Delivery Charge) = "+((Convert.ToDouble(lblPrice.Text)*Convert.ToDouble(lblProductQuantity.Text))+Convert.ToDouble(vat)+Convert.ToDouble(lblDeliveryCharge.Text));

答案 1 :(得分:0)

首先,您要对货币值进行计算 - 因此您应该使用decimal而不是double。前者用于金钱,后者用于科学价值。

因此,从定义的交付值开始:

var delivery = 40m;

现在计算每个项目的值:

var items =
(
    from item in dlCartProducts.Items.Cast<DataListItem>()
    let PriceLabel = item.FindControl("lblPrice") as Label
    let ProductQuantity = item.FindControl("txtProductQuantity") as TextBox
    let price = decimal.Parse(PriceLabel)
    let ProductQuantity = decimal.Parse(ProductQuantity)
    let TotalPrice = price * quantity
    let VAT = Math.Round(TotalPrice * 0.12)
    select new
    {
        ProductQuantity,
        TotalPrice,
        VAT,
    }
).ToArray();

现在您可以轻松计算总数:

var totals = new
{
    ProductQuantity = items.Sum(i => i.ProductQuantity),
    TotalPrice = items.Sum(i => i.TotalPrice + i.VAT) + delivery,
    VAT = items.Sum(i => i.VAT)
};

现在对于现有标签:

Label1.Text = totals.VAT.ToString();
txtTotalPrice.Text = totals.TotalPrice.ToString();
txtTotalProducts.Text = totals.ProductQuantity.ToString();

最后,显示计算的标签:

lblResult.Text = String.Format(
    "{0}(product price) x {1}(product quantity) "
        + "+ vat({2}) + delivery charge({3}) = (total){4}",
    (totals.TotalPrice - delivery - totals.VAT) / totals.ProductQuantity,
    totals.ProductQuantity,
    totals.VAT,
    delivery,
    totals.TotalPrice);