Open In App

Convert Double to Integer in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given a Double real number. Write a Java program to convert the given double number into an Integer (int) in Java.

Examples:

Input: double = 3452.234
Output: 3452

Input: double = 98.23
Output: 98

Double: The double data type is a double-precision 64-bit IEEE 754 floating-point. Its value range is endless. The double data type is commonly used for decimal values, just like float. The double data type also should never be used for precise values, such as currency. Its default value is 0.0.

Example: double d1 = 10.5

Integer: The Integer or int data type is a 32-bit signed two’s complement integer. Its value-range lies between – 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive). Its minimum value is – 2,147,483,648 and maximum value is 2,147,483,647. Its default value is 0. The int data type is generally used as a default data type for integral values unless if there is no problem about memory.

Example: int a = 10

Approaches

There are numerous approaches to do the conversion of Double datatype to Integer (int) datatype. A few of them are listed below.

  • Using TypeCasting
  • Using Double.intValue() method
  • Using Math.round() method 

Approach 1: Using TypeCasting

This technique is very simple and user-friendly.

Syntax:

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

Example:

Java




// Java program to convert Double to
// int using Typecasting
 
public class GFG {
 
    // main method
    public static void main(String args[])
    {
 
        // Get the double value
        double data = 3452.345;
        System.out.println("Double - " + data);
 
        // convert into int
        int value = (int)data;
 
        // print the int value
        System.out.println("Integer - " + value);
    }
}


Output

Double - 3452.345
Integer - 3452

Time Complexity: O(1) as constant operations are used.
Auxiliary Space: O(1) because no extra space is required.

Approach 2: Using Double.intValue() method

This technique is similar to typecasting method. The main difference between typecasting method and this method is that typecasting method is an explicit method, and this method is a Wrapper class Double truncates all digits after the decimal point.

Syntax:

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

Example:

Java




// Java program to convert Double to int
// using  Double.intValue()
public class GFG {
 
    // main method
    public static void main(String args[])
    {
 
        // Get the double value
        Double data = 3452.345;
        System.out.println("Double - " + data);
        // Create a wrapper around
        // the double value
        Double newData = new Double(data);
 
        // convert into int
        int value = newData.intValue();
 
        // print the int value
        System.out.println("Double - " + value);
    }
}


Output:

Double - 3452.345
Double - 3452

Time Complexity: O(1) as constant operations are used.
Auxiliary Space: O(1) because no extra space is required.

Approach 3: Using Math.round() method

Math.round() accepts a double value and converts it into the nearest long value by adding 0.5 to the value and trimming its decimal points. The long value can then be converted to an int using typecasting.

Syntax:

long Math.Round(Double doubleValue);

Example:

Java




// Java program to convert Double to int
// using Math.round()
 
public class GFG {
 
    // main method
    public static void main(String args[])
    {
 
        // Get the double value
        double data1 = 3452.345;
        System.out.println("Double : " + data1);
 
        // convert into int
        int value1 = (int)Math.round(data1);
 
        // print the int value
        System.out.println("Integer : " + value1);
       
          double data2 = 3452.765;
        System.out.println("\nDouble : " + data2);
 
        // convert into int
        int value2 = (int)Math.round(data2);
 
        // print the int value
        System.out.println("Integer : " + value2);
    }
}


Output

Double : 3452.345
Integer : 3452

Double : 3452.765
Integer : 3453

Time Complexity: O(1) as constant operations are used.
Auxiliary Space: O(1) because no extra space is required.

Note: Here you can see that the Math.round() method converts the double to an integer by rounding off the number to the nearest integer. 

For example – 10.6 will be converted to 11 using Math.round() method and 1ill be converted to 10 using typecasting or Double.intValue() method.



Last Updated : 30 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads