How to print with two figures to the right of the decimal.

Hey all. Got a program to run fine, but I'm having to print it in dollars and cents. However, obviously anything other than an int prints multiple places beyond the zero.

So instead of it printing "$34.09667" simply manipulating it to print "$34.10".

I was told to use something like this, assuming x is the variable I'm trying to print in dollars and cents.

 
  cout << setprecision(2) << x << endl;


The variable I'm trying to convert is a double if it helps.
Last edited on
1
2
// fixed point notation, with two digits after the decimal point
std::cout << std::fixed << std::setprecision(2) << x << '\n' ;
It's still printing multiple places next to the decimal.

Check out my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <iomanip>
using namespace std;

// This program is designed to calculate how much interest someone will accumulate given a certain amount of time.
// It will calculate interest accrued after 30 days, 180 days and 360 days.
// The interest rate for this program is 5%, although one could input any percentage.
// Enjoy.
double investment; // Declaring the interest percentage.

// The next three functions calculate the interest for the respective time periods.
void getInterest30(double x) {
	double Interest = 0.05 / 365 * (investment);
	double total30;
	total30 = (x)+(30 * Interest);
	cout << "The total interest accrued for 30 days is $";
	cout << fixed << setprecision(2) << total30 << endl;
}

void getInterest180(double y) {
	double Interest = 0.05 / 365 * (investment);
	double total180;
	total180 = (y) + (180 * Interest);
	cout << "The total interest accrued for 180 days is $";
	cout << fixed << setprecision(2) << total180 << endl;
}

void getInterest360(double z) {
	double Interest = 0.05 / 365 * (investment);
	double total360;
	total360 = (z)+(360 * Interest);
	cout << "The total interest accrued for 360 days is $";
	cout << fixed << setprecision(2) << total360 << endl;
}


int main() {

	cout << "Hey, user! I've been brushing up on my accounting skills!" << endl;
	cout << "I'm going to calculate interest accrued on an investment of yours for three time periods." << endl;
	cout << "But first, I'll need an investment amount. Would you enter one from zero to one hundred, please?" << endl;

	cin >> investment;

	while (investment > 100 || investment < 0) {
		cout << "C'mon, user...work with me here! Read the instructions and enter another one." << endl;
		cin >> investment;
	}

	getInterest30(investment); // Function call for the thirty day interest calculation.

	getInterest180(investment); // Function call for the 180 day interest calculation.

	getInterest360(investment); // Function call for the 360 day interest calculation.
	

	return 0;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <iomanip>

double get_interest( double amount, unsigned int ndays ) {

    static const double annual_interest_rate = 0.05 ;
    static const int days_per_year = 365 ; // leap years?
    static const double per_diem_interest_rate = annual_interest_rate / days_per_year ;

    return amount * per_diem_interest_rate * ndays ;
}

int main() {

    std::cout << "investment amount [1-100]? " ;
    double investment;
    std::cin >> investment;

    std::cout << std::fixed << std::setprecision(2) << "for investment: $" << investment << '\n' ;

    for( unsigned int ndays : { 30, 180, 360 } )
    {
        const double interest = get_interest( investment, ndays ) ;
        std::cout << "after " << std::setw(3) << ndays << " days, interest accrued: $"
                  << interest << "  total: $" << investment + interest << '\n' ;
    }

}

http://coliru.stacked-crooked.com/a/fbee495449082806
Last edited on
Topic archived. No new replies allowed.