Reputation: 11
double calc_percent_of_daily_sugar(int sugar) {
double result;
result = sugar/avg_sugar_intake;
return result*100;
}
I'm trying to get this function to return the result to only 2 decimal places however when I try to use cout << setprecision(2) << result;
then there are still more than 2 places in the number
Apologies if this is a nooby or stupid question.
Upvotes: 0
Views: 2207
Reputation: 141890
*How to print a “double” value to two decimal places in base 10 in C++
Use std::fixed
to print it in decimal.
std::cout << std::setprecision(2) << std::fixed << 123.4567 << std::endl;
// 123.46
See: https://en.cppreference.com/w/cpp/io/manip/fixed
Upvotes: 1