This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting and number formats

 
Sara A
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I compiled I got an error saying that I was using incompatible types
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic