title: Converting double to integer in Java
body: <p>In Java, I want to convert a double to an integer, I know if you do this:</p>
  
  <pre><code>double x = 1.5;
  int y = (int)x;
  </code></pre>
  
  <p>you get y=1. If you do this:</p>
  
  <pre><code>int y = (int)Math.round(x);
  </code></pre>
  
  <p>You'll likely get 2. However, I am wondering: since double representations of integers sometimes look like 1.9999999998 or something, is there a possibility that casting a double created via Math.round() will still result in a truncated down number, rather than the rounded number we are looking for (i.e.: 1 instead of 2 in the code as represented) ?</p>
  
  <p>(and yes, I do mean it as such: Is there <em>any</em> value for x, where y will show a result that is a truncated rather than a rounded representation of x?)</p>
  
  <p>If so: Is there a better way to make a double into a rounded int without running the risk of truncation?</p>
  
  <hr>
  
  <p>Figured something: Math.round(x) returns a long, not a double. Hence: it is impossible for Math.round() to return a number looking like 3.9999998. Therefore, int(Math.round()) will never need to truncate anything and will always work.</p>