Results 1 to 4 of 4

Thread: Java double to int

  1. #1
    Join Date
    Apr 2009
    Posts
    79

    Java double to int

    Java converts a ints to doubles automatically but i want to convert the double for comparing. If i divide an int numerator by an int denominator, like 2/5, and wanted the result to be in a decimal, .40, how would you turn it into a double?

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Java double to int

    Double.intValue() converts a Double to an int. Double.intValue() converts a Double to an int. I don't think there's any way to go directly from a double to an int. Java does not let us assign a floating-point value (float or double) directly to an integer variable (byte, short, int or long).

  3. #3
    Join Date
    Jan 2009
    Posts
    199

    Re: Java double to int

    Convert Double to Integer in Java :

    Code:
    Import java.lang.Math.*;
    
    public class java
    {
    public static void main(String avg[])
    {
    int d=0;
    try
    {
    Double e=(940.0/1000.0)*100;
    d=e.intValue();
    }catch(Exception f){}
    System.out.println(d);
    }
    }

  4. #4
    Join Date
    Dec 2008
    Posts
    177

    Re: Java double to int

    Code:
    public class javaintdouble  {
    
        public static void main(String[] avg){
            
            double intdbl = 430.5;
            
            //Type cast double to int
            int j = (int)intdbl;
            
            System.out.println(j);
        }        
    }

Similar Threads

  1. Replies: 4
    Last Post: 04-09-2013, 11:04 PM
  2. Replies: 7
    Last Post: 28-12-2011, 05:18 PM
  3. Replies: 4
    Last Post: 25-09-2009, 05:09 PM
  4. Link List Example in Java Sample program in Java
    By trickson in forum Software Development
    Replies: 2
    Last Post: 04-08-2009, 08:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,423,501.74530 seconds with 17 queries