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?

4

7 Answers

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.

1

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.

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", ...).

6

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: "; } } 

When you are trying to store values upto n decimal values in a variable . You have to multiple that value with 10^n and divide the same with 10^n. Afterward use type operator to manipulate in the program. Here is the example : -

 float a,b,c,d,sum; cin>>a>>b>>c>>d; // reading decimal values sum=(a*b*c*d); sum=round(sum*100)/100; // here it is for 2 decimal points if((float)sum < (float) 9.58) cout<<"YES\n"; else cout<<"NO\n"; 

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?

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.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like