Results 1 to 5 of 5
  1. #1
    tyang is offline Member
    Join Date
    Jan 2010
    Posts
    44
    Rep Power
    0

    Default How to convert a double into a int?

    How to convert a double into a int?

    Heaps of thanks for your kindly help :)

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    by explicit casting.

    Java Code:
    double x = 5.0;
    int a = (int) x;
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  3. #3
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    cast it into an int type like this:
    double a = 5.89;
    int b = (int)a;

    if you do this, b = 5;

  4. #4
    tyang is offline Member
    Join Date
    Jan 2010
    Posts
    44
    Rep Power
    0

    Default

    Thank you!

  5. #5
    thomasdevos's Avatar
    thomasdevos is offline Member
    Join Date
    Feb 2010
    Posts
    19
    Rep Power
    0

    Default

    hi tyang,
    below code can help you.
    Java Code:
    public class Convert {
    	public static void main(String[] args) {
    		double x = 88.33;
    		int y = (int) x;
    		System.out.println(y);
    	}
    }
    but one thing should be kept in your mind that converting form double 2 int will lost the pricision, some time, lost a lot.
    take this code on your pc to see the result:
    Java Code:
    public class Convert {
    	public static void main(String[] args) {
    		double x = Double.MAX_VALUE;
    		System.out.println(x);
    		int y = (int) x;
    		System.out.println(y);
    	}
    }
    i hold 7 years develop exp. now i start a thread to share my knowlege about a j2ee project. welcome to participate.Study Java Through Real Java Project

Similar Threads

  1. convert String to Double
    By azurovyhrosik in forum CLDC and MIDP
    Replies: 5
    Last Post: 10-22-2008, 02:46 AM
  2. Replies: 7
    Last Post: 01-12-2008, 09:02 PM
  3. convert string to a double?
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-27-2007, 03:10 AM
  4. Replies: 2
    Last Post: 11-16-2007, 01:24 PM
  5. Help with convert a double type number
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •