Write a Java Program to convert double type variables to int

Java Program to convert double type variables to int

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to convert double type variables to int
Java Program to convert double type variables to int

Write a Java Program to convert double type variables to int

PROGRAM: Java Program to convert double type variables to int

/*Java Program to convert double type variables to int*/

1: Java Program to Convert double to int using Typecasting

class Main {
public static void main(String[] args) {

// create double variables
double a = 23.78D;
double b = 52.11D;

// convert double into int
// using typecasting
int c = (int)a;
int d = (int)b;

System.out.println(c); // 23
System.out.println(d); // 52
}
}

2: Convert double to int using Math.round()

class Main {
public static void main(String[] args) {

// create double variables
double a = 99.99D;
double b = 52.11D;

// convert double into int
// using typecasting
int c = (int)Math.round(a);
int d = (int)Math.round(b);

System.out.println(c); // 100
System.out.println(d); // 52
}
}

3: Java Program to Convert Double to int

class Main {
public static void main(String[] args) {

// create an instance of Double
Double obj = 78.6;

// convert obj to int
// using intValue()
int num = obj.intValue();

// print the int value
System.out.println(num); // 78
}
}


		

Leave a Reply

Your email address will not be published. Required fields are marked *