• Post Reply Bookmark Topic Watch Topic
  • New Topic

Double to Integer Conversion  RSS feed

 
Sally Jenkins
Greenhorn
Posts: 17
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to convert a double to an integer.

I found and tried different approaches. Below is the code that I selected:



Although I know the differences between int and double, are there any other concerns I should have when conducting this conversion?

Also, I know this code works, however is there a more efficient method to conduct this conversion?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More efficient in terms of memory, time, or someone comprehending your intention when they read this for the first time?

My larger point is that you should not worry about efficiency unless and until you have a documented, specific requirement and your performance testing shows you are failing it.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Careful about capitalisation; int integer and Integer are different things. An int is a primitive datatype representing a two's complement whole number in 32 bits. An Integer is an object encapsulating an int. An integer is a generic term for a whole number.

If you convert a double to an int, the result will be in the range −2³¹…2³¹ − 1. The range of a double is much bigger, and you can find the largest and smallest values in the Documentation. So you have lots of double numbers outwith the range of int values.
I think the simplest way to do that sort of conversion is to use a cast, officially called a narrowing primitive conversion, and you can read all about it in the Java® Language Specification (=JLS). Beware: the JLS can be difficult to understand. As far as I can remember, numbers within the int range are converted by losing their fractional part, which is called rounding towards zero. (Unofficially called truncation.) Numbers outwith the int range are converted to the nearest value (maximum or minimum). I can't remember what happens to a NaN; it might turn into 0. Try this sort of code:-You require () because cast has a higher precedence than +.

Also look at the Math#round() method.
 
Sally Jenkins
Greenhorn
Posts: 17
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. These are exactly the types of answers I was looking for. I wasn't sure what a cast was but your explanation helps a lot.
I asked because I was a bit concerned in regards to time and comprehension. I also wanted to investigate in regards to intentions as well. The most difficult part of learning anything new (and I do mean anything) is that you attempt to construct a whole through using pieces of other people's examples without fully understanding what your are using.

Campbell, I really like your example. Thank you for your help. I'll see if I can rework my code accordingly since I have a better understanding of what I am being asked to do.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!