Rounding

I need to find a function to round the numbers to the second decimal place only...here is my code that I need to figure out.
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
// Andrew Driscoll

#include <iostream.h>
int main ()
{
	int rcup, ctr=0;
	float price, gross=1, subtot, total;
	char first[10], date[10];
	cout << "First Name: "; cin >> first; cout << "\n";
	cout << "(01/01/1001) Date: "; cin >> date; cout << "\n"; 
	cout << "How many Reeses Cups are you buying?\n"; cin >> rcup;
	while (ctr<rcup)
	{
		cout << "What is the price of the Reeses Cups?\n"; cin >> price;
		gross=gross+price;
		ctr++;
	}
	subtot=gross*.06; total=subtot+gross;
	cout << "\n"; cout << "\n"; cout << "\n";
	cout << "Cashier: " << first << "\n"; cout << "Date: " << date << "\n";
	cout << "Tax: " << subtot << "\n"; cout << "Subtotal: " << gross << "\n"; cout << "Total: " << total << "\n";
	cout << "Have a nice day!\n";
	return 0;
}
		
/* 
First Name: Andrew

(01/01/1001) Date: 07/02/9999

How many Reeses Cups are you buying?
2
What is the price of the Reeses Cups?
1.19
What is the price of the Reeses Cups?
2.18



Cashier: Andrew
Date: 07/02/9999
Tax: 0.2622			<-- Only want first two decimals.
Subtotal: 4.37		
Total: 4.6322       <-- Only want first two decimals.
Have a nice day!
Press any key to continue
*/
Last edited on
You need to use Manipulators.

http://www.cplusplus.com/reference/iostream/manipulators/

Try reading about setprecision(int n).
Ok, I attempted it but now I have a stupid error.
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
// Andrew Driscoll

#include <iostream.h>
#include <iomanip>


int main ()
{
	int rcup, ctr=0;
	double price, gross=1, subtot, total;
	char first[10], date[10];
	cout << "First Name: "; cin >> first; cout << "\n";
	cout << "(01/01/1001) Date: "; cin >> date; cout << "\n"; 
	cout << "How many Reeses Cups are you buying?\n"; cin >> rcup;
	while (ctr<rcup)
	{
		cout << "What is the price of the Reeses Cups?\n"; cin >> price;
		gross=gross+price;
		ctr++;
	}
	subtot=gross*.06; total=subtot+gross;
	cout << "\n"; cout << "\n"; cout << "\n";
	cout << "Cashier: " << first << "\n"; cout << "Date: " << date << "\n";
	cout << setprecision (2) << "Tax: " << subtot << "\n"; cout << setprecision (2) << "Subtotal: " << gross << "\n"; cout << setprecision (2) << "Total: " << total << "\n";
	cout << "Have a nice day!\n";
	return 0;
}
		
/* 
--------------------Configuration: CashRegister - Win32 Debug--------------------
Compiling...
CashRegister.cpp
I:\C++\CashRegister.cpp(24) : error C2065: 'setprecision' : undeclared identifier
Error executing cl.exe.

CashRegister.exe - 1 error(s), 0 warning(s)

*/
Try adding

using namespace std;

at the top of the file. I'm actually somewhat surprised that your compiler is not complaining about the cout statements, since you haven't specified it, and your not clarifying it with std::cout. Dev-C++ complains that <iostream.h> is an antiquated header, too. What compiler are you using?
No clue, its 2005 C++ from high school.
It doesn't complain about cout, because you included <iostream.h> instead of <iostream>. You should change it, and explicitly use namespace std.
Last edited on
About the actual question:

I think it's a really bad idea to use floating point numbers as currency. Unfortunately It's often used in tutorials aswell.

My suggestion is to use integers to represent the amounts as hundreds, and implement IO yorself.

Example:
1
2
int value = 250; // 2.50
cout << value / 100 << '.' << value % 100;


To simplify code, I would encapsulate this in a class, and implement iostream operators.
Last edited on
Sorry I didn't have time to give a more detailed explanation the last time. Here is an some example code (note that it has limitations. e.g. it would print 5.05 as 5.5 and the maximum value is about 40 million)

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
60
61
#include <iostream>
using namespace std;


class Currency {
public:
    Currency() {
        val_ = 0;
    }

    Currency(int v) {
        val_ = v;
    }

    Currency operator+(Currency other) {
        return Currency(val_ + other.val_);
    }

    Currency operator-(Currency other) {
        return Currency(val_ - other.val_);
    }

    Currency operator*(int f) {
        return Currency(val_ * f);
    }

    friend ostream& operator<<(ostream& os, Currency c) {
        return os << c.val_ / 100 << '.' << c.val_ % 100;
    }

    friend istream& operator>>(istream& is, Currency& c) {
        is >> c.val_;
        c.val_ *= 100;
        char ch;
        is >> ch;
        int v;
        is >> v;
        c.val_ += v;
        return is;
    }
    
private:
    int val_;
};

int main() {
    cout << "How many Reeses Cups are you buying?" << endl;
    int rcup = 0;
    cin >> rcup;
    
    cout << "What is the price of the Reeses Cups?" << endl;
    Currency price;
    cin >> price;
    
    Currency total = price * rcup;

    cout << "Total price is " << total << endl;
    cout << "Have a nice day!" << endl;
    return 0;
}


Output:
1
2
3
4
5
6
// How many Reeses Cups are you buying?
// 5
// What is the price of the Reeses Cups?
// 2.50
// Total price is 12.50
// Have a nice day! 
I think this was quite interesting, so I tried to improve the stream operators. Here are the results:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

    friend ostream& operator<<(ostream& os, Currency c) {
        std::div_t d = std::div(c.val_, 100);
        std::div_t dd = std::div(d.rem, 10);
        return os << d.quot << '.' << dd.quot << dd.rem;
    }

    friend istream& operator>>(istream& is, Currency& c) {
        is >> c.val_;
        c.val_ *= 100;
        if (is.peek() == '.') {
            is.get(); // Skip period
            if (std::isdigit(is.peek())) {
                c.val_ += (is.get() - '0') * 10;
                if (std::isdigit(is.peek())) {
                    c.val_ += (is.get() - '0');
                }
            } else {
                is.unget();
            }
        }
        return is;
    }

how about using printf??
use it in the way
printf("%1.2f", number);
this would give you the number with atleast 1 digit before decimal and exactly 2 digits after decimal....
It would, but floating point values are not really the right tool for representing currency. They are great for continuos values representing physical things like length, speed, weight etc. But currency is really a discrete value, and should be represented by a discrete data type.

You never want an amount of money to be 0.00000000000001 or 1.5e49.

There is a real danger with using floats for currency:
If you have something like 100,500.50, and add a large number (like a billion, I don't know exactly), precision information WILL be lost. You will probably end up with something like 1.00001e9. This equals one 1 billion + 100,000. In other words, you lost 500.50.
Topic archived. No new replies allowed.