• 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

converting a double to an int

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I'm converting a double to an integer, does it just take off all the numbers beyond the decimal point? (for example if I converted 1.4235 to an integer, would I just get a result of 1)
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct. It essentially does a truncate, not a round. So if you convert 1.999 to an int you'd still end up with 1. And you have to explicitly cast it or you'll get a compile error.
double dd = 1.995;
int ii = (int)dd; // correct
int jj = dd; // compile error
In the java.lang.Math class are several convenience methods for doing things like "floor()", "trunc()", "round()", etc., if you want to do something else.
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I also need to truncate a number, and I went and looked under java.lang.Math like you said and I cant find a trunc() method.
I see round, floor, ceil, but no trunc(), what do you guys have been using to truncate a double value ?
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what I get for quickly throwing up an answer w/out double-checking with the documentation. My bad: there is no "trunc()" method.
If you want to truncate a "double" value there are a couple of options:

This gives you the output:
DD1: 1234.5678
II1: 1234
DD2: 1234.0
DD2: 1234.56
So you can truncate and store the result as an "int" (such as "ii1"), or store the result as a double (such as "dd2"). Or you can truncate to two digits (such as in "dd3").
Because of the way in which real numbers are stored, you might truncate and instead of coming out with "1234.56" as here, you could get "1234.55999999999999". If you want to ensure you what gets printed out only has a certain number of digits, use the "java.text.DecimalFormat" class to do your formatting.
[ November 10, 2003: Message edited by: Wayne L Johnson ]
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also use the java.text.DecimalFormat class to 'truncate' a double. Look it up and if you need an example, say so and I'll post some.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic