0

My code:

cout.setf(ios_base::fixed, ios_base::floatfield);
		plan++;
		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.precision(0);
		cout << setw(10) << principal << "\t";
		cout.precision(1);
		cout << setw(7) << rate << "%" << "\t"
		<< setw(8) << years << "\t";
		
		cout.precision(2);
		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

2.5
5
3.5
2

instead of

2.5
5.0
3.5
2.0

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?

3
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by inputbear
0

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

static_cast<int>double x;
0

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 by inputbear: n/a

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.