Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

how to divide two ints and get a double java

/*Now, there are several ways we can try to get precise 
double value (where num and denom are int type)*/

//with explicit casting:
double d = (double) num / denom;
double d = ((double) num) / denom;
double d = num / (double) denom;
double d = (double) num / (double) denom;
//but not double d = (double) (num / denom);

//with implicit casting:
double d = num * 1.0 / denom;
double d = num / 1d / denom;
double d = ( num + 0.0 ) / denom;
double d = num;    d /= denom;
/*but not double d = num / denom * 1.0;
and not double d = 0.0 + ( num / denom );*/
 
PREVIOUS NEXT
Tagged: #divide #ints #double #java
ADD COMMENT
Topic
Name
1+4 =