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

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?

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 …

## All 2 Replies

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.

Be a part of the DaniWeb community

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