August 1st, 2003, 09:34 AM
Pls help with simple calculation
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 ?
August 1st, 2003, 10:42 AM
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.
August 1st, 2003, 04:35 PM
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
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
August 1st, 2003, 09:15 PM
Typecasting your answer as an int will simply take the interger portion of the number. Try Math.Round(<var>, 0);