• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Casting and number formats

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I compiled I got an error saying that I was using incompatible types
reply
    Bookmark Topic Watch Topic
  • New Topic