Convert Double to Integer in Java


Given a Double real number, the task is to convert it into Integer in Java.

Examples:

Input: double = 3452.234
Output: 3452

Input: double = 98.23
Output: 98
  1. Using typecasting: This technique is a very simple and user friendly.

    Syntax:



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

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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;
      
            // convert into int
            int value = (int)data;
      
            // print the int value
            System.out.println(value);
        }
    }

    chevron_right

    
    

    Output:

    3452
    
  2. Using Math.round(): This method returns the nearest integer.

    Syntax:

    double data = 3452.645
    int value = (int)Math.round(data);
    

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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 data = 3452.345;
      
            // convert into int
            int value = (int)Math.round(data);
      
            // print the int value
            System.out.println(value);
        }
    }

    chevron_right

    
    

    Output:

    3452
    
  3. Using Double.intValue(): This technique is similar to typecasting method. Wrapper class Double truncates all digits after decimal point.

    Syntax:

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

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to convert Double to int
    // using using Double.intValue()
      
    public class GFG {
      
        // main method
        public static void main(String args[])
        {
      
            // Get the double value
            double data = 3452.345;
      
            // 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(value);
        }
    }

    chevron_right

    
    

    Output:

    3452
    


  4. My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.