Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate discount price

Tags:

c#

winforms

I want to make my application to calculate a discount price. This is how I find my discount price, but I have a little problem (logic problem):

private void UpdateDiscount(object sender, EventArgs e)
{
    decimal actualPrice = 0;
    decimal discount = 0;
    int calculateDiscount = 0;
    int totalDiscount = 0;
    int afterDiscount = 0;
    int totalAfterDiscount = 0;
    int total = 0;

    if (numericTextBox1.TextLength == 6)
    {
        this.numericUpDown2.Enabled = true;

        discount = Convert.ToInt32(this.numericUpDown2.Value);

        calculateDiscount = Convert.ToInt32(discount / 100);

        totalDiscount = calculateDiscount;

        if (!string.IsNullOrEmpty(this.numericTextBox3.Text.ToString()))
        {
            actualPrice = Convert.ToDecimal(this.numericTextBox3.Text);
        }

        else
        {
            numericTextBox3.Text = "";
        }

        afterDiscount = Convert.ToInt32(actualPrice * totalDiscount);
        totalAfterDiscount = Convert.ToInt32(actualPrice);
        total = Convert.ToInt32(totalAfterDiscount - afterDiscount);

        if (numericUpDown2.Value > 0)
        {
            this.numericTextBox6.Text = total.ToString();
        }
    }

    else if (numericTextBox1.TextLength != 6)
    {
        this.numericUpDown2.Enabled = false;

        this.numericUpDown2.Value = 0;
        this.numericTextBox6.Text = "";
    }

    else
    {
        actualPrice = 0;
        discount = 0;
        calculateDiscount = 0;
        totalDiscount = 0;
        afterDiscount = 0;
        totalAfterDiscount = 0;
        total = 0;

        MessageBox.Show("There is no data based on your selection", "Error");
    }
}

This is the result, the total after discount still same with the total price, even though I already give it discount value.

enter image description here

like image 780
Kaoru Avatar asked Aug 30 '25 16:08

Kaoru


2 Answers

Given

  • a price P such that 0 <= P, and
  • a discount percentage D such that 0 <= D <= 100

you can compute the discount (markdown) MD that needs to be applied as

MD = P * (D/100)

You can then get the discounted price DP as

DP = P - MD

Given that, this should do you:

public static class DecimalHelpers
{
  public static decimal ComputeDiscountedPrice( this decimal originalPrice , decimal percentDiscount )
  {
    // enforce preconditions
    if ( originalPrice   <   0m ) throw new ArgumentOutOfRangeException( "originalPrice"   , "a price can't be negative!"    ) ;
    if ( percentDiscount <   0m ) throw new ArgumentOutOfRangeException( "percentDiscount" , "a discount can't be negative!" ) ;
    if ( percentDiscount > 100m ) throw new ArgumentOutOfRangeException( "percentDiscount" , "a discount can't exceed 100%"  ) ;

    decimal markdown        = Math.Round( originalPrice * ( percentDiscount / 100m) , 2 , MidpointRounding.ToEven ) ;
    decimal discountedPrice = originalPrice - markdown ;

    return discountedPrice ;
  }
}
like image 106
Nicholas Carey Avatar answered Sep 02 '25 10:09

Nicholas Carey


Don't use int (or Convert.ToInt32) when dealing with money.

See this

decimal discount = 10;
var calculateDiscount = Convert.ToInt32(discount / 100);
MessageBox.Show(calculateDiscount.ToString());

calculateDiscount will be 0 because 0.1 will be converted 0.

like image 44
L.B Avatar answered Sep 02 '25 08:09

L.B