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

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.

Integers don't have decilam places. You'd need to convert the variables to double.

include Iomanip and use cout << fixed << setprecision(2)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.