#1
  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. #2
  3. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep 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.
  4. #3
  5. 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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

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

IMN logo majestic logo threadwatch logo seochat tools logo