title: Most languages fail to round 31.45 correctly
body: <p>We have this input field where the user is supposed to enter a percentage. We display the percentage with 1 decimal. If the user choose to enter 31.45 we noticed that the float representation is 31.44999999999 which is correctly formatted as 31.4, but proper math rounding rules says <em>the original numeric value</em> should be rounded to 31.5.</p>
  
  <p>So I looked for similar questions but most answers says that the string formatters handle should handle this case correctly. However it does not for me.</p>
  
  <p>I tried the following C-code:</p>
  
  <pre><code>#include &lt;math.h&gt;
  #include &lt;stdio.h&gt;
  
  int main(int argc, char* argv[])
  {
      float x = 31.45;
  
      printf("%.20f\n", x);
      printf("%.3f\n", x);
      printf("%.2f\n", x);
      printf("%.1f\n", x);
      printf("%.0f\n", x);
  
      double y = 31.45;
  
      printf("%.20f\n", y);
      printf("%.3f\n", y);
      printf("%.2f\n", y);
      printf("%.1f\n", y);
      printf("%.0f\n", y);
  
      return 0;
  }
  </code></pre>
  
  <p>The output:</p>
  
  <pre><code>31.45000076293945312500
  31.450
  31.45
  31.5
  31
  31.44999999999999928946
  31.450
  31.45
  31.4
  31
  </code></pre>
  
  <p>Note that float handles it correctly, while double doesn't!</p>
  
  <p>I tried the same thing in Python:</p>
  
  <pre><code>&gt;&gt;&gt; "%.1f" % 31.45
  '31.4'
  </code></pre>
  
  <p>I tried with Wolfram Alpha: <a href="http://www.wolframalpha.com/input/?i=round+31.45+to+nearest+.1" rel="nofollow">http://www.wolframalpha.com/input/?i=round+31.45+to+nearest+.1</a>: same result.</p>
  
  <p>NSNumberFormatter in Obj-C also has the same problem.</p>
  
  <p>Excel handles it correctly though! 31.45 is formatted as 31.5 when using one decimal.</p>
  
  <p>I'm not sure what to think of this. I was imagining that the string formatters would be smart enough to handle the limitations of floating point values, but it seems most do not. Any hints how to handle this in a generic way? Also, is there any logic to why floats behave better than double in this case, or is it just "luck"?</p>