title: DecimalFormat and rounding for fixed point numbers
body: <p>While working with <a href="http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html" rel="nofollow"><code>DecimalFormat</code></a>, I was puzzled by its behavior when rounding numbers with fixed-point patterns. To make things more specific:</p>
  
  <pre><code>double n = 2082809.080589735D;
  
  // Output the exact value of n
  System.out.println("value:       " + new BigDecimal(n).toPlainString());
  
  System.out.println("double:      " + n);
  
  DecimalFormat format = new DecimalFormat("0.00000000");
  
  System.out.println("format:      " + format.format(n));
  System.out.println("format (BD): " + format.format(new BigDecimal(n)));
  </code></pre>
  
  <p>The output of this snippet is:</p>
  
  <pre><code>value:       2082809.080589734949171543121337890625
  double:      2082809.080589735
  format:      2082809.08058974
  format (BD): 2082809.08058973
  </code></pre>
  
  <p>From the first output line we notice that the actual value is <em>below</em> the half-way point between <code>2082809.08058973</code> and <code>2082809.08058974</code> (<code>...49...</code>). Despite this, <code>DecimalFormat</code> rounds the value upwards when provided with a <code>double</code> argument.</p>
  
  <p>Other values are rounded downwards:</p>
  
  <pre><code>value:       261285.2738465850125066936016082763671875
  double:      261285.273846585
  format:      261285.27384658
  format (BD): 261285.27384659
  </code></pre>
  
  <p>This does not happen in all cases:</p>
  
  <pre><code>value:       0.080589734949171543121337890625
  double:      0.08058973494917154
  format:      0.08058973
  format (BD): 0.08058973
  
  value:       0.2738465850125066936016082763671875
  double:      0.2738465850125067
  format:      0.27384659
  format (BD): 0.27384659
  </code></pre>
  
  <p>It seems to me that the formatted string for a <code>double</code> is rounded using <a href="http://en.wikipedia.org/wiki/Rounding#Round_half_to_even" rel="nofollow">half-even rounding</a> based on an imprecise decimal value produced with something along the lines of <code>Double.toString()</code>, rather than the actual mathematical value represented by the <code>double</code> in question. When the formatted precision is very close to (or more than) the precision provided by the <code>double</code> type, things start becoming somewhat random.</p>
  
  <p>In all the cases presented above, formatting the corresponding <code>BigDecimal</code> seems to perform the rounding as expected.</p>
  
  <p>I was unable to find any specification describing the proper behavior of <code>DecimalFormat</code> in this case.</p>
  
  <ul>
  <li><p>Is this behavior documented somewhere?</p></li>
  <li><p>From a correctness point of view, wouldn't rounding the actual mathematical value be preferable?</p>
  
  <p>I understand that people writing <code>1.0...35</code> would (naively?) expect it to be rounded to <code>1.0...4</code>, but <code>1.0...35</code> may not even be representable in any primitive data type available in Java...</p></li>
  </ul>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I have submitted a report to Oracle - hopefully they will be able to either fix or clarify this issue.</p>