Results 1 to 2 of 2
  1. #1
    Avionix is offline Member
    Join Date
    Dec 2014
    Posts
    3
    Rep Power
    0

    Default Not getting decimal for a surely decimal answer

    I'm using eclipse. I'm going to get straight to the point and give all the info I can, if the values in the first code box are used, shouldn't these values be left after all in the second box is done:
    remainder=23, arr[0]=100, div=23/10=2.3, whole=2, and decimal=3?
    When I use this code, div comes out to be just (2.0).
    Java Code:
    int leng=10;
    arr[0]=123; //int
    arr[1]=100; //int
    Java Code:
    if (arr[0]!=arr[1]){
    	int remainder=arr[0]-arr[1];
    	arr[0]=arr[0]-remainder;
    	double div=remainder/leng;  //double div=Double.valueOf(remainder/leng);
    	int whole=(int) Math.floor(div);
    	int decimal=(int) ((div-whole)*leng);
    I'm not sure were I'm going wrong in how div is being calculated, but I ultimately need div to be 2.3.
    I've also used the second option commented out which still gives (2.0).
    If anyone needs more info, I don't mind giving the whole project.
    p.s. If its a really simple math error I'm missing, please tell.
    Last edited by Avionix; 01-01-2015 at 05:15 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Not getting decimal for a surely decimal answer

    When you divide two integers the result is an integer, even though you assign the result to a double. So cast either remainder or leng to a double.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 1
    Last Post: 11-19-2013, 10:25 PM
  2. Replies: 5
    Last Post: 08-25-2011, 07:32 PM
  3. Decimal to binary, octal to decimal
    By matejm1994 in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 09:59 AM
  4. converting a decimal to an int
    By shuks in forum New To Java
    Replies: 9
    Last Post: 10-12-2009, 09:41 AM
  5. decimal calculations?
    By arnab321 in forum CLDC and MIDP
    Replies: 5
    Last Post: 11-19-2008, 03:36 AM

Posting Permissions

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