title: Casting floating point (double) to string
body: <p>I'm running into something I've never seen before and would very much like to understand what is going on. I'm trying to round a double to 2 decimal places and cast that value to a string. Upon completion of the cast things go crazy on me.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>void formatPercent(std::string&amp; percent, const std::string&amp; value, Config&amp; config)
  {
      double number = boost::lexical_cast&lt;double&gt;(value);
      if (config.total == 0)
      {
          std::ostringstream err;
          err &lt;&lt; "Cannot calculate percent from zero total.";
          throw std::runtime_error(err.str());
      }
      number = (number/config.total)*100;
      // Format the string to only return 2 decimals of precision
      number = floor(number*100 + .5)/100;
      percent = boost::lexical_cast&lt;std::string&gt;(number);
  
      return;
  }
  </code></pre>
  
  <p>I wasn't getting quite what I expected so I did some investigation. I did the following:</p>
  
  <pre><code>std::cout &lt;&lt; std::setprecision(10) &lt;&lt; "number = " &lt;&lt; number &lt;&lt; std::endl;
  std::cout &lt;&lt; "percent = " &lt;&lt; percent &lt;&lt; std::endl;
  </code></pre>
  
  <p>...and get the following:</p>
  
  <pre><code>number = 30.63
  percent = 30.629999999999999
  </code></pre>
  
  <p>I suspect that boost is doing something funny. Does anyone have any insight here?</p>
  
  <p>Seriously, how strange is this?!? I ask for 10 digit precision on a double and get 4 digits. I ask to cast those 4 digits to a string and get that mess. What is going on?</p>