Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
August 1st, 2003, 09:34 AM
 coder
Junior Member

Join Date: Jul 2003
Posts: 2
Time spent in forums: < 1 sec
Reputation 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
August 1st, 2003, 10:42 AM
 Onslaught
/(bb|[^b]{2})/

Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 5,163
Time spent in forums: 6 Days 1 h 34 m 20 sec
Reputation Power: 792
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
August 1st, 2003, 04:35 PM
 coder
Junior Member

Join Date: Jul 2003
Posts: 2
Time spent in forums: < 1 sec
Reputation 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
August 1st, 2003, 09:15 PM
 Nem
Contributing User

Join Date: Jun 2003
Posts: 109
Time spent in forums: < 1 sec
Reputation Power: 11
Typecasting your answer as an int will simply take the interger portion of the number. Try Math.Round(<var>, 0);

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Pls help with simple calculation