Question

[Solved] C++ round a double up to 2 decimal places

I am having trouble rounding a GPA double to 2 decimal places. (ex of a GPA needed to be rounded: 3.67924) I am currently using ceil to round up, but it currently outputs it as a whole number (368)

here is what I have right now

if (cin >> gpa) {
    if (gpa >= 0 && gpa <= 5) {
           // valid number

           gpa = ceil(gpa * 100);

           break;
    } else {
           cout << "Please enter a valid GPA (0.00 - 5.00)" << endl;
           cout << "GPA: ";

    }
}

using the above code with 3.67924 would output 368 (which is what I want, but just without the period between the whole number and the decimals). How can I fix this?

Solution #1:

To round a double up to 2 decimal places, you can use:

#include <iostream>
#include <cmath>

int main() {
    double value = 0.123;
    value = std::ceil(value * 100.0) / 100.0;
    std::cout << value << std::endl; // prints 0.13
    return 0;
}

To round up to n decimal places, you can use:

double round_up(double value, int decimal_places) {
    const double multiplier = std::pow(10.0, decimal_places);
    return std::ceil(value * multiplier) / multiplier;
}

This method won’t be particularly fast, if performance becomes an issue you may need another solution.

Respondent: kaveish

Solution #2:

If it is just a matter of writing to screen then to round the number use

std::cout.precision(3);
std::cout << gpa << std::endl;

see

floating points are not exactly represented so by internally rounding the value and then using that in your calculations you are increasing the inexactness.

Respondent: AndersK

Solution #3:

Try this. But your cout statement in else condition, so it won’t give the desired output for 3.67924.

if (cin >> gpa)
{     
    if (gpa >= 0 && gpa <= 5) {
        // valid number

        gpa = ceil(gpa * 100);
        gpa=gpa/100;
        break;
    } 
    else
    {    
       cout << "Please enter a valid GPA (0.00 - 5.00)" << endl;    
       cout << "GPA: ";
    }
}
Respondent: MD. Khairul Basar

Solution #4:

You can’t round doubles to two decimal places. Doubles don’t have decimal places. They have binary places, and they aren’t commensurable with decimal places.

If you want decimal places, you must use a decimal radix, e.g. when formatting for output with printf(“%.2f”, …).

Respondent: user207421

Solution #5:

Example: you want 56.899999999999 to be output as a string with 2 decimal point which is 56.89.

First, convert them
value = 56.89 * 1000 = 5689
factor = 100
– 1 decimal point = 10
– 2 decimal point = 100
– 3 decimal point = 1000
etc

int integerValue;
int decimal;
std::string result;
function ( int value , int factor)
{
    integerValue= (value / factor) * factor; //(5689 / 100) * 100 = 5600
    decimal = value - integerValue;  // 5689 - 5600;
    result = std::to_string((int)(value/factor) + "." + std::to_string(decimal); 
    // result = "56" + "." + "89" 
    // lastly, print result
}

Not sure if this can help?

Respondent: Goh Allant

Solution #6:

std::string precision_2(float number)
{
    int decimal_part = (number * 100) - ((int)number * 100);
    if (decimal_part > 10) {
        return std::to_string((int)number) + "." + std::to_string(decimal_part);
    } else {
        return std::to_string((int)number) + ".0" + std::to_string(decimal_part);
    }
}

Handles well for all positive floats. A minor modification will make it work for -ves as well.

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy