wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Casting and number formats Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Casting and number formats" Watch "Casting and number formats" New topic
Author

Casting and number formats

Sara A
Greenhorn

Joined: Jun 16, 2004
Posts: 2
I am attempting to convert a decimal number back to an integer for use on a map that users update with polygons, the polygon uses integers and i tried to cast the variable back to an int but its not working. I've tried the decimalformat, double.parsedouble etc. Here is a code snipet, I restored it to it's original format so you wont see anything i have tried, I'm new to java so hopefully someone here can answer if not i'll try the intermediate or advanced forums. Thanks

public void updateLatLon() {
map.clearPolysNoRedraw();
areaPoly = new Polygon();
areaPoly.npoints = 4;
//XXX rounded off
int [] xpoints = new int[4];
int [] ypoints = new int[4];
xpoints[0] = (int)(panel.getNW().getLat() * 100);
ypoints[0] = (int)(panel.getNW().getLon() * 100);
xpoints[1] = (int)(panel.getNE().getLat() * 100);
ypoints[1] = (int)(panel.getNE().getLon() * 100);
xpoints[2] = (int)(panel.getSE().getLat() * 100);
ypoints[2] = (int)(panel.getSE().getLon() * 100);
xpoints[3] = (int)(panel.getSW().getLat() * 100);
ypoints[3] = (int)(panel.getSW().getLon() * 100);

areaPoly.xpoints = (int) xpoints;
areaPoly.ypoints = (int) ypoints;
map.draw(areaPoly);
}
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Remember Sara, when you typecast a double into an int it will round up above 1.0. So if is below that then you will get zeros.

The correct way to type cast a double is (int)(.6 + .2)
This will give you 0 as an answer as an int.
but (int)(.8 + .4) will give you an answer of 1 as an int.

HTH

Gabe
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Sara Cantsay,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

Obviously fictitious names aren't allowed.

Thanks Pardner! Hope to see you 'round the Ranch!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
i tried to cast the variable back to an int but its not working

What does "its not working" mean? Did you get a runtime result that you didn't expect? What was it? What had you expected? Did the compiler have something to say? What exactly?

When asking folks a question, taking a minute to first explain things to your Cardboard Analyst (also called Rubber Ducking) and to think about Asking a Good Question is time well spent.
Sara A
Greenhorn

Joined: Jun 16, 2004
Posts: 2
When I compiled I got an error saying that I was using incompatible types
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting and number formats