### I need help rounding interest

I had to make an if else program calculate interest for a class, well just to return the percentage you'd receive but I'm adding the interest earned for it. I used decimals for the interest and when I calculate the interest I get returned more than two decimals since money works as 0000.00 so I need to round it to 0000.00.

Code:

/**********************************
Program 8 First National Bank of Gmiller
Brandon youngblood
**********************************/
#include <iostream>
using namespace std;
int main ()
{
//Variables declared. Variable d is 1/12 of the year interest for a monthly
//rate
double a;
double b = .0134;
double c = .015;
double d = .0833;

//Greeting statement
cout << "Welcome to the First National Bank of Gmiller!" << endl <<

cin >> a;
//If else statement used so that 2000 dollars is factored in along with
//anything more than 2000 dollars.
//Also did interest for the year and for a single month; Becuase both have
//practical uses.
if (a < 1999.99)
{
cout << "Your interest rate is 1.34% and you'll get "
<< a * b * d << " per month" << endl;
cout << "Your interest is " << a * b << " per year" << endl << endl;
}
else if (a > 2000)
{
cout << "Your interest is rate 1.5% and you'll get "
<< a * c * d << " per month" << endl;
cout << "Your interest is " << a * c << " per year" << endl;
}
//Then for the off chance case of 2000 being in an account or 1999.99
//I made an if else statement to accomodate that
if (a == 1999.99)
{
cout << "Your interest is rate is 1.34% and you'll get "
<< a * b * d << " per month" << endl;
cout << "Your interest is " << a * b << " per year" << endl;
}
else if (a == 2000)
{
cout << "Your interest is rate is 1.5% and you'll get "
<< a * c * d << " per month" << endl;
cout << "Your interest is " << a * c << " per year" << endl;
}

}
You sir, just need to set the precision:
First include `<iomanip>` at the top of your code
And then anywhere above where you want to set the precision put in this line of code:
`cout << setiosflags(ios::fixed) << setprecision(2);`
The '2' in the parenthesis declares how many numbers may go beyond the decimal point
I hope this was of help
~Ciao
Thanks it helped!
Topic archived. No new replies allowed.