d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Question About Code
Add Reply New Topic New Poll
Member
Posts: 31,359
Joined: Mar 25 2009
Gold: 15.00
Jul 26 2018 06:19pm
Code
public class DecimalComparator {
public static boolean areEqualByThreeDecimalPlaces(double x, double y) {

if((int) (x * 1000) == (int) (y * 1000)) {
return true;
}
return false;
}
}



I don't understand what (int) does.....?
it it for making double into int, type casting?

This post was edited by ferf on Jul 26 2018 06:33pm
Member
Posts: 4,748
Joined: Sep 2 2014
Gold: Locked
Jul 27 2018 03:27am
it casts it to int type
Member
Posts: 4,748
Joined: Sep 2 2014
Gold: Locked
Jul 27 2018 03:29am
Quote (SmITexX @ Jul 27 2018 11:27am)
it casts it to int type


edit
casting to int doesn't round but only cuts off decimal digits
Member
Posts: 11,273
Joined: Apr 26 2008
Gold: 3,303.50
Jul 28 2018 11:27am
Cast to int by truncating!
By the way if you have this pattern

Code
if (cond) {
return true;
}
else {
return false;
}


You can always change it to

Code
return cond;


In your case, the body of the function could simply be

Code

return ((int) (x * 1000) == (int) (y * 1000));


This post was edited by xapeu on Jul 28 2018 11:29am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll