This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Casting and number formats" Watch "Casting and number formats" New topic

Casting and number formats

Sara A

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() {
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;
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.


Dirk Schreckmann

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

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

Joined: Jun 16, 2004
Posts: 2
When I compiled I got an error saying that I was using incompatible types
It is sorta covered in the JavaRanch Style Guide.
subject: Casting and number formats
Similar Threads
draw a triangle
PathIterator returns the last point twice?
loops in java to produce shapes
Trying to use an existing java.awt.Polygon in my JSP
Good User Interface