Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

How to Convert Double to Int in Java

Last Updated on April 19, 2023 by Prepbytes

In Java, double is a data type that represents floating-point numbers, whereas int is a data type that represents integers. Sometimes, it may be necessary to convert a double to an int, such as when we need to perform arithmetic operations that only work with integers or when we want to store the result in an int variable. This article explores various methods to convert double to int in Java. So, without any further delay, let’s move to our section which is different approaches to convert double to int in Java.

Approaches to Convert double to int in Java

Now, let’s discuss in detail all the approaches to convert double to int in Java:

Approach 1: Using TypeCasting in Java

Type Casting in Java is the simplest way to convert double to int in Java. It involves explicitly converting a data type to another data type using the syntax (datatype) variable. When converting a double to an int using type casting, the decimal part of the double value is truncated, i.e., it is simply removed.

Syntax of TypeCasting in Java

double data = 52.6345
int value = (int)data;

Now, let’s look at an example that converts double to int in Java using TypeCasting.

Code Implementation:

class PrepBytes {
    public static void main(String args[])
    {
        double data = 52.6345;
        System.out.println("Double - " + data);
        
        int value = (int)data;

        System.out.println("Integer - " + value);
    }
}

Output:

Double - 52.6345
Integer - 52

Explanation: In the above Java example, we declare a class named "PrepBytes" with a "main" method. The "main" method starts by creating a double variable "data" and assigning it the value 52.6345. Next, our Java program converts the double variable "data" to an integer variable "value". After that, we print the value of "value" to the console.

Approach 2: Using Math.round() Method

When we use the Math.round() method to convert a double to int in Java, the decimal part of the double value is rounded to the nearest long value by adding 0.5 and trimming the decimal points. Typecasting can then be used to transform the long value to an int.

Syntax of Math.round() Method

double doubleValue = 52.6345;
int intValue = (int) Math.round(doubleValue);

Now, let’s look at an example that convert double to int in Java using Math.round() method.

Code Implementation:

class PrepBytes {
    public static void main(String args[])
    {
        double data1 = 52.6345;
        System.out.println("Double : " + data1);

        int value1 = (int)Math.round(data1);

        System.out.println("Integer : " + value1);
    }
}

Output:

Double: 52.6345
Integer: 53

Explanation: In the above example, we declare a class named "PrepBytes" with a "main" method. The "main" method begins by creating a double variable named "data1" and assigning it the value of 52.6345. Next, the program uses the Math.round() method to round the double value of "data1" to the nearest integer value. The rounded value is then explicitly cast to an integer variable "value1" using an explicit cast.

Approach 3: Using Double.intValue() Method

The Double class in Java provides a method called intValue() that returns the int value of a Double object. We can use this method to convert a double value to an int value. When we use the Double.intValue() method to convert a double to an int, the decimal part of the double value is truncated, i.e., it is simply removed.

Syntax of Double.inValue() method

double data = 52.6345
Double newData = new Double(data);
int value = newData.intValue();

Now, let’s look at an example that converts double to int in Java using the Double.intValue() method.

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

        Double data = 52.6345;
        
        System.out.println("Double - " + data);
        
        Double newData = new Double(data);

        int value = newData.intValue();

        System.out.println("Double - " + value);
    }
}

Output:

Double - 52.6345
Double - 52

Explanation: In the above example, we created a class named "PrepBytes" with a "main" method. The "main" method starts by creating a Double variable "data" and assigning it the value 52.6345. Next, the program creates a new Double object "newData" by passing "data" as a parameter to the constructor of the Double class. The program then converts the Double object "newData" to an integer value using the intValue() method, which returns the integer value of the Double object.

Conclusion
There are several ways to convert a double to int in Java. Type casting, using the Math.round() method, and using the Double.intValue() method are the most commonly used methods. Type casting is the simplest method, while the Math.round() method is useful when we need to round off the decimal part of the double value to the nearest integer. The Double.intValue() method is useful when we need to simply truncate the decimal part of the double value. By understanding these methods, we can easily convert a double to int in our Java programs.

FAQs

Here are some frequently asked questions on how to convert double to int in Java.

Q1: What is the simplest way to convert double to int in Java?
Answer: The simplest way to convert double to int in Java is by using type casting, which involves explicitly converting a data type to another data type using the syntax (datatype) variable.

Q2: What happens when we use type casting to convert double value that is too large to fit to int in Java?
Answer: When we use type casting to convert a double value that is too large to fit into an int in Java, the result is undefined and may lead to an overflow or underflow exception.

Q3: What is the advantage of using the Math.round() method over type casting when trying to convert double to int in Java?
Answer: The advantage of using the Math.round() method over type casting when trying to convert double to int in Java is that it allows us to round off the decimal part of the double value to the nearest integer.

Q4: What is the Double class in Java used for?
Answer: The Double class in Java is a wrapper class that provides methods and constants for working with double values.

Q5: What is the advantage of using the Double.intValue() method over type casting when trying to convert double to int in Java?
Answer: The advantage of using the Double.intValue() method over type casting when trying to convert double to int in Java is that it allows us to work with Double objects and provides more flexibility when working with double values.

Leave a Reply

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