Muhammad Azeem Khan
Muhammad Azeem Khan

Reputation: 91

How Get Discount-Percentage In C#

let suppose dis.text = 2, prc.text = 100, I am using these codes.It Should be net_prc.text = 98.But its giving me -100.Can anybody tell me why?,And how can i get correct discounted percentage??

 private void net_prcTabChanged(object sender, EventArgs e)
    {
        int d;
        int di;
        int i;
        d = Convert.ToInt32(dis.Text);
        i = Convert.ToInt32(prc.Text);
        di = -((d / 100) * i) + i;
        net_prc.Text = di.ToString();
    }

Upvotes: 0

Views: 11403

Answers (7)

jerryurenaa
jerryurenaa

Reputation: 4704

it is easier if you do number * 0.95 to discount 5 percent for example discountedPrice= currentPrice * 0.95;

Upvotes: 0

Muhammad Azeem Khan
Muhammad Azeem Khan

Reputation: 91

Even dis.text = 1.5

private void net_prcTabChanged(object sender, EventArgs e)
    {
        double d;
        double di;
        double i;
        d = Convert.ToDouble(dis.Text);
        i = Convert.ToDouble(prc.Text); 
        di = -((d * 100.0) / i ) + i;
        net_prc.Text = di.ToString();
    }

Upvotes: 0

Jeppe Stig Nielsen
Jeppe Stig Nielsen

Reputation: 61952

Your division, d / 100, is a division of integers, and it returns an integer, probably 0 (zero). This is certainly the case with your example d = 2.

Addition: If you really want to do this with integers (rather than changing to decimal or double like many other answers recommend), consider changing the sub-expression

((d / 100) * i)

into

((d * i) / 100)

because it will give you a better precision to do the division as the last operation. With the numbers of your example, d=2 and i=100, the first sub-expression will give 0*100 or 0, while the changed sub-expression yields 200/100 which will be 2. However, you will not get rounding to nearest integer; instead you will get truncating (fractional part is discarded no matter if it's close to 1).

Upvotes: -1

christopher
christopher

Reputation: 27346

di = -((d / 100) * i) + i;

All values in this statement are Integers. You are going to be computing arithmetic with decimal places, and you need to increase the precision of your variables to a double or a float. Instead, add a decimal place to one of the values in the equation. This will force all values into doubles.

This is a process called Arithmetic Promotion. It is where, at run time, the precision of every variable in an equation is increased to the size of the most precise variable.

Upvotes: 1

SteveP
SteveP

Reputation: 19093

Try (d / 100.0) to force it to use floating point arithmetic

Upvotes: 1

bizzehdee
bizzehdee

Reputation: 21003

C# has an odd way of doing maths, because your numbers are cast as integers, you can only do integer math with them. you need to initially have them as float or as double so you can do float math or anything at all that requires a decimal place within the calculations.

Upvotes: 0

Parimal Raj
Parimal Raj

Reputation: 20575

Proper way to do it would be, changing the datatype of di to float

di = (d * 100) / i;

Upvotes: 0

Related Questions