Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round Double and Cast to String

Tags:

c++

I suppose this question is a follow up to a previous question I had regarding casting a double to a string.

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:

void formatPercentCommon(std::string& percent, const std::string& value, Config& config)
{
    double number = boost::lexical_cast<double>(value);
    if (config.total == 0)
    {
        std::ostringstream err;
        err << "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<std::string>(number);

    return;
}

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?

Thanks in advance for the help. :)

like image 733
Rico Avatar asked Sep 14 '25 02:09

Rico


2 Answers

Streams are the usual formatting facility in C++. In this case, a stringstream will do the trick:

std::ostringstream ss;
ss << std::fixed << std::setprecision(2) << number;
percent = ss.str();

You are probably already familiar with setprecision from your previous post. fixed here is used to make the precision affect the number of digits after the decimal point, instead of setting the number of significant digits for the whole number.

like image 105
Matti Virkkunen Avatar answered Sep 15 '25 16:09

Matti Virkkunen


I haven't tested this, but I believe that the following should work:

string RoundedCast(double toCast, unsigned precision = 2u) {
    ostringstream result;
    result << setprecision(precision) << toCast;
    return result.str();
}

This uses the setprecision manipulator to change the precision of the ostringstream that is doing the conversion.

like image 45
templatetypedef Avatar answered Sep 15 '25 17:09

templatetypedef