# Thread: weird double/int question (very short code)

1. ## weird double/int question (very short code)

Hi~~~
I'm new to the forums (and C++), and hope someone can help me with a very basic program. I apologize if any etiquette has been violated.

The following is a very basic money sorting programs that works*, in which a given amount is broken up to its maximum amount of dollars, quarters, etc. (I left out nickels and pennies to shorten this post).

Code:
```/*Money Sorter*/
#include "iostream"
#include "cmath"
using namespace std;

int main()
{
double amount;
cin>>amount;
int dollars, quarters, dimes, nickels, pennies;

dollars=int(amount);                    //represents amount in full dollars
quarters=(int)((amount-dollars)/.25);    //leftover quarters
cout<<dollars<<" "<<quarters<<endl;

dimes=(int)((amount-dollars-quarters*.25) /.1 );   //dimes left
cout<<dimes<<endl;

system("pause");
return 0;

}```

*So I believe this program is good with one exception. When I typed in \$15.20, it gives you 15 dollars, 0 quarters, and 1 dime. I'm not sure why or how I can fix it to be 2. My best guess is that quarter is ultimately a double, which messes up the intent of the italicized portion.

To be honest, I'd rather just move on and learn other things, but I thought this was weird enough for me to sign on to the forums and ask for an opinion.

Cheers!

2. Floating point - Wikipedia, the free encyclopedia
The first thing you need to know is that floats are approximations of true values.
It also makes floats useless as a data type for money.

So whilst you might assign 15.20, it might even print 15.20, but mathematically, you have the nearest approximate value of 15.19999999.

Normally, the change giving program is best done in integer arithmetic, where you start with 1520 cents (for example).

3. Try putting a +.5 before casting to an int. The thing with casting to integers is that the rounding is weird. Instead of rounding up for certain values, it conditionally rounds down. ie (int)1.9 == 1

Code:
```quarters=(int)((amount-dollars)/.25 + .5);    //leftover quarters
dimes=(int)((amount-dollars-quarters*.25) /.1 + .5);   //dimes left```

4. thank you guys!

yes, (int) does act like (floor) I think; ah, the +.5 might be a good trick or just using integers will be more practical;

I will try to redo as best as I can ^^

5. Originally Posted by Minty
thank you guys!

yes, (int) does act like (floor) I think; ah, the +.5 might be a good trick

I will try to redo as best as I can ^^
I don't think so. Casting rounds down to zero whereas floor rounds down. Meaning the two do the same for positive numbers, but the opposites for negative numbers.

6. thanks EVOEx

yeah, I'm still having trouble with redoing the casting.... oh boy ;/

I'm just gonna move on since I can't seem to untangle the double from the int, even when I try to set all variables to be integers;

there are other C++ money sorter programs that rely on while statements, etc., so maybe that would be more appropriate

7. Originally Posted by Salem
Floating point - Wikipedia, the free encyclopedia
The first thing you need to know is that floats are approximations of true values.
It also makes floats useless as a data type for money.

So whilst you might assign 15.20, it might even print 15.20, but mathematically, you have the nearest approximate value of 15.19999999.

Normally, the change giving program is best done in integer arithmetic, where you start with 1520 cents (for example).
Yes! When working with money always work in integer pennies...
Whole accounting packages are written that way for good reason.

Floating point math is always an approximation of the actual value. Integers don't guess.

8. Casting to int does not round; it truncates (is throws away the bits containing the information of the decimals, regardless of what value they are).