title: How does java round an integer when stored in a floating point
body: <p>Its a classic problem: your legacy code uses a floating point when it should really be using n integer.  But, its to expensive to change every instance of that variable (or several) in the code.  So, you need to write your own rounding function that takes a bunch of parameters to improve accuracy and convert to an integer.</p>
  
  <p>So, the basic questions is, how do floating point numbers round when they are made in java?  the classic example is 0.1 what is often quoted as rounding to 0.0999999999998 (or something like that).  But does a floating point number always round down to the next value it can represent when given an integer in Java?  Does it round down it's internal mantissa to efficiently round down its absolute value?  Or does it just pick the value with the smallest error between the integer and the new float?</p>
  
  <p>Also is the behavior different when calling <code>Float.parseFloat(String)</code> when the String is an integer like <code>"1234567890"</code>?  And is the behavior also the same when String is a Floating point with more precision than the Float can store.</p>
  
  <p>Please note that I use floating point or reference Float, I use that interchangeably with Double.  Same with integer and long.   </p>