0

Hey guys i need a bit of help in getting my answer correct. Basically , im trying to get my answer to 2 decimal places when i divide one number by another...

int main(){
int Totalgpa = 46;
int e = 13;

int SemesterGpa = Totalgpa / e ;

   cout << SemesterGpa << endl;
}

The answer i would get will just be 3 as it will round off the answer.

So i tried another method i read from here

int main () {
int Totalgpa = 46;
int e = 13;

double SemesterGpa = double Totalgpa / e ;

   cout << SemesterGpa << endl;
}

The answer i would get will just be 3.5384615348.......

How can i modify my coding in order for the answer to just be 3.54 ( to 2 decimal places only).

4
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by Avatar99
0

You could mess around with scaling your integers so that you only produce the required number of decimal places or you could just use the io manipulators from <iomanip> to format the output of the double value to only show 2 decimal places cout << fixed << setprecision(2) << SemesterGpa << endl; .

personally in this case I would probably do the second.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.