Results 1 to 3 of 3
  1. #1
    jebbo is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default Trying to store a decimal between 0&1 after dividing by 100

    Hi, I'm not totally new to Java, been playing for a while but I've hit an embarassing brick wall. The answer to this must be simple but I just can't get it!!!

    Basically I'm playing with making a little text based game. Each enemy has an amount of damage it can do, and I'm trying to make it relate to it's health.

    ie a damage of 10 and 100 health returns 10
    but a damage of 10 and 50 health returns 5.

    I've tried to divide the health by 100 to get a decimal value then multiply by the damage to lower it sufficiently but for some reason Java stores a value of 0 when I do the divide by 100. Here's some code i quickly tested to check I wasn't going mad (the real game has loads of methods that would need explaining to get the values):

    System.out.println(groups.jake.getHealth()); //correctly outputs value 50
    double healthTest = groups.jake.getHealth()/100; //should store 0.5
    System.out.println(healthTest); //outputs 0.0 instead of 0.5

    What's going on?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Integer arithmetic. Either have getHealth() return double, use a denominator that is a double literal or cast either the numerator or denominator to double to indicate to the compiler that floating point arithmetic is to be done.
    Java Code:
    double healthTest = groups.jake.getHealth()/100.0;
    db

    db

  3. #3
    jebbo is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default

    Haha how simple, just make the 100 a double, probably would've stumbled across that thought pattern sooner or later - I can feel the egg running down my face!

    Thanks, sure I'll be posting more soon

Similar Threads

  1. Dividing in Java
    By hawaiifiver in forum New To Java
    Replies: 19
    Last Post: 03-11-2011, 11:09 AM
  2. Java source code for Dividing a file
    By LOKESHWARI in forum Advanced Java
    Replies: 2
    Last Post: 10-14-2010, 03:27 PM
  3. Store the decimal number into an variable
    By fataguila in forum New To Java
    Replies: 4
    Last Post: 02-01-2010, 07:22 PM
  4. dividing video files into frames..
    By swathi palla in forum AWT / Swing
    Replies: 0
    Last Post: 02-15-2009, 03:39 PM
  5. Dividing numbers with remainders showing
    By denisdoherty in forum New To Java
    Replies: 16
    Last Post: 04-24-2008, 05:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •