title: losing precision converting from java BigDecimal to double
body: <p>I am working with an application that is based entirely on doubles, and am having trouble in one utility method that parses a string into a double.  I've found a fix where using BigDecimal for the conversion solves the issue, but raises another problem when I go to convert the BigDecimal back to a double: I'm losing several places of precision.  For example:</p>
  
  <pre><code>import java.math.BigDecimal;
  import java.text.DecimalFormat;
  
  public class test {
      public static void main(String [] args){
          String num = "299792.457999999984";
          BigDecimal val = new BigDecimal(num);
          System.out.println("big decimal: " + val.toString());
          DecimalFormat nf = new DecimalFormat("#.0000000000");
          System.out.println("double: "+val.doubleValue());
          System.out.println("double formatted: "+nf.format(val.doubleValue()));
      }
  }
  </code></pre>
  
  <p>This produces the following output:</p>
  
  <pre><code>$ java test
  big decimal: 299792.457999999984
  double: 299792.458
  double formatted: 299792.4580000000
  </code></pre>
  
  <p>The formatted double demonstrates that it's lost the precision after the third place (the application requires those lower places of precision).</p>
  
  <p>How can I get BigDecimal to preserve those additional places of precision?</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <p>Update after catching up on this post.  Several people mention this is exceeding the precision of the double data type. Unless I'm reading this reference incorrectly:
  <a href="http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3" rel="noreferrer">http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3</a>
  then the double primitive has a maximum exponential value of E<sub>max</sub> = 2<sup>K-1</sup>-1, and the standard implementation has K=11.  So, the max exponent should be 511, no?</p>