Wednesday, December 31, 2014

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.




No comments:

Post a Comment