-3

I have little problem, and I need help..

So here is my problem I have created win form in c# and used numericupdown element to insert my numbers, but I cant calculate percent. Here is the code below:

private void button8_Click(object sender, EventArgs e)
    {
        int x, y, sum;
        x = Convert.ToInt16(numericUpDown7.Value);
        y = Convert.ToInt16(numericUpDown8.Value);
        sum = x * 3.4528 + 21%;
        textBox5.Text = Convert.ToString(sum);
    }

What I need to do is to insert x and press button to calculate this formula

example: x * 3.4528 + 21 % = ???

Maby someone has options to help me.

Thanks for all of you, who will help me!

Wilfredo P
  • 4,070
  • 28
  • 46
zte813
  • 95
  • 1
  • 10
  • 1
    sum cannot be an integer if you multiply it by a float or double.. – Gmnd-i Oct 08 '14 at 11:35
  • 1
    You might want to explain what your code is doing a bit more. Like what is `numericUpDown7`, what is `y` and why do you never use it? What is the actual problem you are having? Does this not compile, not run, give you the wrong answer? Also I am not sure what the "+21%" means even... – Chris Oct 08 '14 at 11:35
  • 1
    In C# the `%` character is an operator that performs the [mod](http://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx) operation. It has nothing to do with percentages. – Adam H Oct 08 '14 at 11:37
  • What my code is supposed to do is to calculate ex.: 29.02 Eur convert to LTL, but i need to add VAT 21% so.. 29.02 * 3.4528 + 21% (in simple calculator style) But I need to do this with one button click and numericupdown7 insert number... its same as textbox.. – zte813 Oct 08 '14 at 11:41
  • Adding 21% is the same as multiplying by 1.21, so you could just do `* 1.21` – Adam H Oct 08 '14 at 11:46

3 Answers3

0

Try this

sum = (x * 3.4528) * 1.21;
David Pilkington
  • 13,528
  • 3
  • 41
  • 73
  • This one was right! thanks, else problem was I have choosen bad numericUpDown7 it is numericUpDown9, thanks for the help! – zte813 Oct 08 '14 at 11:44
0

private void button1_Click(object sender, EventArgs e) { double eng, urdu, math, cs, tot, per;

        eng = Convert.ToDouble(txtenglish.Text);
        urdu = Convert.ToDouble(txturdu.Text);
        math = Convert.ToDouble(txtmath.Text);
        cs = Convert.ToDouble(txtcs.Text);

        tot = eng + urdu + math + cs;
        lbltotal.Text = Convert.ToString(tot);

        per = (tot / 400) * 100;
        lblpercent.Text = Convert.ToString(per);
    }
  • 1
    When answering questions on stackoverflow, you should provide context or explanation of your code, writing only code is not enough. – Basel Issmail Mar 06 '19 at 17:39
-1

First off you need to use decimal, float, or double instead of int (you can find many references online about each to help you determine which would be best for you). Otherwise it will just truncate the answer and drop anything after the decimal point. Second you need to use the formula that everyone else has mentioned sum = x * 3.4528 * 1.21.

juharr
  • 31,741
  • 4
  • 58
  • 93