0

I'm got write a simple calculation. which only use % and / .but when i key in different number. the result is different.Who can help me.

private void button1_Click(object sender, EventArgs e)
        {
            
            decimal x = txtPer.Value;
            decimal y = txtQty.Value;

            decimal j= y / x;
            decimal k = y % x;
            

                txtPack.Value = j;
                txtRemain.Value = k;
            

        }

From the picture, we can see that 10/3=3 and 10%3=1 is right.
But I key in 5/2=3 5%2=1 is wrong. It should be 5/2=2.
If 5/2=3 5%2=1,we multiply and plus back will become 2*3+1=7. The answer is wrong

Edited by lianpiau: n/a

Attachments wrong.JPG 42.99 KB
1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by lianpiau
0

I'm got write a simple calculation. which only use % and / .but when i key in different number. the result is different.Who can help me.

private void button1_Click(object sender, EventArgs e)
        {
            
            decimal x = txtPer.Value;
            decimal y = txtQty.Value;

            decimal j= y / x;
            decimal k = y % x;
            

                txtPack.Value = j;
                txtRemain.Value = k;
            

        }

From the picture, we can see that 10/3=3 and 10%3=1 is right.
But I key in 5/2=3 5%2=1 is wrong. It should be 5/2=2.
If 5/2=3 5%2=1,we multiply and plus back will become 2*3+1=7. The answer is wrong

I'm already solve problem. Just convert from decimal to int.
for example, int a = Convert.ToInt32(txtQty.Value);
this is work.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.