My code:

cout.setf(ios_base::fixed, ios_base::floatfield);
		double Annually, Monthly, Daily;
		Annually = CompoundAnnually (principal, rate, years);
		Monthly = CompoundMonthly (principal, rate, years);
		Daily = CompoundDaily (principal, rate, years);
		cout << setw(3) << plan << "\t";
		cout << setw(10) << principal << "\t";
		cout << setw(7) << rate << "%" << "\t"
		<< setw(8) << years << "\t";
		cout << setw(11) << Annually << "\t"
		<< setw(11) << Monthly << "\t"
		<< setw(11) << Daily << "\t"
		<< endl;

I had to set the decimal precision for all of these after doing it for these last three where it's necessary, because this chunk of code is within a while loop, so next time it comes around, the stuff up top is affected, which is not right. I want the rates to appear like this


instead of


but it looks like I have to make a choice between no decimal or one absolutely. UNLESS of course there is a way to close this cout.setf setting thing. Help please?

You could just cast the numbers you want no precision on with

static_cast<int>double x;

And if you don't exactly know if the number that will be output is a whole number you can check with something like:

if(x - (int)x == 0){
    cout << (int)x << endl;

Although, I'm not sure if a whole number will always be represented as "x.0", so maybe a rounding function could do instead of casting to check if the number is whole e.g:(1.0).

Then you could output it to the screen by casting it to int like L3gacy stated.

Edit: Checking for whole numbers by rounding would defeat the purpose of having floating numbers, sorry bad suggestion.

Edited 5 Years Ago by inputbear: n/a

This article has been dead for over six months. Start a new discussion instead.