Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

C++ question involving setprecision() manipulator.?

I am trying to set up a program that will show the amount of a loan with cents, and also show the monthly interest rate. By default, the floating point value is displayed with six significant digits, so I used << fixed << setprecision(2) to show the cents of the loan amount. The problem i am running into is in resetting it so that the extra zeros are dropped off of the end of the monthly interest rate. the six significant figures are fine there (I don't mind if it rounds the rate at the sixth decimal place), but if I use setprecision(6) before the monthly_rate, it will always show 6 sig figs (including extra zeros.) I have to place the rate after the loan amount, and since the rate will change according to the APR entered, I'd like it to treat the monthly_rate as if the <<fixed<<setprecision(2) was never entered above it. (ie, lets take the APR's of 1.2 and 5.25, respectively. when those are entered individually, i'd like the program to spit out .1 and .4375, not .100000 and .437500.) Basically, I want the dang thing to reset i guess (for lack of a better description) before displaying the monthly_rate, because by default it will display that float value to 6 significant digits AND drop the 0's at the end. Please help!

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

double loan_amount, monthly_rate, APR;

loan_amount = 150000.25;

cout << "What is the APR?" << endl;

cin >> APR;

monthly_rate = APR/12;

cout << fixed << setprecision(2) << "The amount is $" << loan_amount << endl;

cout << "The monthly interest rate is " << monthly_rate << "%\n";

system("pause");

return 0;

}

The following didn't work, because it will always display 6 digits including zeros:

cout << setprecision(6) << "The monthly interest rate is " << monthly_rate << "%\n";

and, since the rate will change according to the APR, I can't use a setprecision() for a set number. (ie, APR = 5.25 will give monthly_rate of .4375. if I use setprecision(4), then this number will look correct, but an APR of 1.2 will give monthly_rate displayed as .1000. I want that to display as .1, and .4375 to display as .4375.)

There are no answers yet.
Be the first to answer this question.