Cast double to integer
public class CastingDemo {
public static void main(String[] argv) {
int i, ans;
double d = 2.75;
i = d; // EXPECT COMPILE ERROR
i = (int)d; // with cast; i gets 2
System.out.println("i =" + i);
ans = (int)d * 3; // truncate d before multiplying
System.out.println("ans =" + ans);
ans = (int)(d * 3); // multiplies before truncating
System.out.println("ans =" + ans);
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
Data Type Double:
- Cast double to integer
- Create Double from double value
- Compare two double type variables within epsilon
- Compare double value arrays for almost equal
- Convert double to string
- Convert Double to numeric primitive data types
- Format double to percentage
- Is Double Infinite
- Is double positive infinity
- Is Double Not a Number(NaN)
- Round a double using BigDecimal
- Round double half up
- Min and Max value fo double type