This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Converting a Double to an Int Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Converting a Double to an Int" Watch "Converting a Double to an Int" New topic
Author

Converting a Double to an Int

wayne drummond
Greenhorn

Joined: Jan 15, 2001
Posts: 9
I know I'll loose some accuracy ,however can someone give me an example of the syntax. Thanks
Kathy Rogers
Ranch Hand

Joined: Aug 04, 2000
Posts: 103
I'm not really sure whether you're talking about objects (Double and Integer) or primitive datatypes (double and int). But here's a short program which illustrates both.

The important things here are - to cast to an int, you need to put (int) before the double you want to cast to an int. And to get the int value of a Double object, you can use the intValue() method of the Double object.
Hope this helps,
Kathy
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 514
Use the wrapper classes. Converting primatives is much easier if you move them into the wrapper classes. Here's an example of how to do so
Double MyDoubleObject = new Double(1.002);
Integer MyIntegerObject = new Integer((MyDoubleObject.intValue()));
System.out.println(MyIntegerObject.toString());


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
wayne drummond
Greenhorn

Joined: Jan 15, 2001
Posts: 9
Thanks guys. However, Kathy your solution is more along the lines of what I was looking for.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Converting a Double to an Int