### Thread: Pls help with simple calculation

1. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2003
Posts
2
Rep Power
0

#### Pls help with simple calculation

Hi,
I am trying to calculate a simple discount on a variable with defining percentage and the value comes out to be 0 all th etime. I am not sure what I am doing wrong can anyone pls suggest?

I define my variable in class:

public int per;

then I am calling per with different values :

per= 0.02 or 0.04 depending on my qty .

my problem is
0.02 * anything gives me 0 value .

I tried replacing 0.02 by 2%(anything) still same result .

can someone pls tell me what is wrong and how can i convert 0.02 into soem other format ?
2. The problem is that the variable that you have defined is an int(eger) which only accepts whole numbers. If you assign a value of 0.02 to an integer they you are assigning 0

Try using a float/double data type instead of int.

Also, your subject title is unacceptable. You can refer to the sticky thread at the top of this forum for more information on how to post a question.
3. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2003
Posts
2
Rep Power
0

#### Conversion of Datatype from double to int

Sorry about the subject I didnt read how to . But pls help me even after changing I am not getting exact value.

double discountrate = 2; double discountamt = (double)(unitcost * discountrate * quantity) /100 ;
double grandtotal = (unitcost * quantity) - discountamt;

int up = (int)(grandtotal/quantity);

Lets say here unicost=15 and quantity=20 so my calculation goes:

discountamt= double(15 * 20 * 2) /100
grandtotal= (15*20)- 6
up= (294/20)

I want the result to be 14.70 . What should I use as convert for up?

If I make up as double it gives me 15 as result and if I keep it int and it gives me 14 .

Pls let me know where am I wrong
4. No Profile Picture
Nem
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2003
Posts
109
Rep Power
14
Typecasting your answer as an int will simply take the interger portion of the number. Try Math.Round(<var>, 0);