Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Basically i try to calculate taxed from cart total value that are getting from database, but the value is not updating while display at lblTotal.text. There is no error occurred so I dont know why its not updating . here is my code

What I have tried:

C#
protected void Page_Load(object sender, EventArgs e)
{
    using (ShoppingCartActions usersShoppingCart = new ShoppingCartActions())
    {
        float cartTotal, taxedvalue;
        cartTotal = (float)usersShoppingCart.GetTotal();
        taxedvalue = 6 / 100 * cartTotal;
        cartTotal += taxedvalue;
        if (cartTotal > 0)
        {
            // Display Total.
            lblTotal.Text = String.Format("{0:c}", cartTotal += taxedvalue);
        }
        else
        {
            LabelTotalText.Text = "";
            lblTotal.Text = "";
            ShoppingCartTitle.InnerText = "Shopping Cart is Empty";
            UpdateBtn.Visible = false;
            CheckoutImageBtn.Visible = false;
        }
        
    }
}
Posted
Updated 9-Nov-20 20:16pm
v2
Comments
Sandeep Mewara 9-Nov-20 14:29pm    
Did you debug and see? Is the code going to if or else part?

1 solution

You are performing integer division before the multiply, so 6 / 100 == 0. Use this:
float taxedvalue = (6.0f / 100.0f) * cartTotal;
Note: this will also work:
float taxedvalue = cartTotal * 6 / 100;
because the compiler recognizes the first parameter as a float and automatically treats the two integer values as floats when it multiplies and divides.
 
Share this answer
 
v3
Comments
Maciej Los 10-Nov-20 2:27am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900