Calculate discount price
c#, winforms
Solution
I suspect the problem is mostly due to the fact that you're using integers for everything. In particular, these two lines:
discount = Convert.ToInt32(this.numericUpDown2.Value);
calculateDiscount = Convert.ToInt32(discount / 100);
When you use 10 for "10%" as the discount, the second line there is actually resulting in a zero. This is because you are doing integer mathematics: integers can only be whole numbers, and when they have a number that is not whole they truncate it. In this case, discount / 100 in your example would be 0.1, which would get truncated to zero.
Instead of using int, I recommend using decimal for all financial transactions. I would replace most of your integer variable types throughout that function with decimal.
Problem
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.