1. Member
Join Date
Dec 2014
Posts
3
Rep Power
0

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. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

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

Posting Permissions

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