Round Double and Cast to String
<p>I suppose this question is a follow up to a previous question I had regarding casting a double to a string.</p>

<p>I have an API where I'm given a string which represents a number.  I need to round this number to 2 decimals of precision and return it as a string.  My attempt to do this follows:</p>

<pre><code>void formatPercentCommon(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>Unfortunately the cast captures "unrounded" values.  (i.e. number = 30.63, percent = 30.629999999999)  Can anyone suggest a clean way to round a double and cast it to a string so I get what one would naturally want?</p>

<p>Thanks in advance for the help.  :)</p>
