I am writing a program about giving change. But I am having issues with getting remainders for decimal numbers.

For instance I am giving \$46.63 change

I want to know how many \$20.00 bills it has, I would have to divide it by 20.

46.63 / 20 = 2.3315

My question is how can I get the .3315?

The problem is that % does not work with double, I tried it with integer, but I just can't get the cents.

## All 8 Replies

so convert everything to cents
multiply up by 100 convert to int, use %, then convert results back to double

commented: Exactly! +17

fmod() is for doubles

so convert everything to cents
multiply up by 100 convert to int, use %, then convert results back to double

Multiply the \$46.63 by 100 that's what you mean.

Would it be like this 46.63 * 100 % 20?

46.63 / 20 = 2.3315

No. double x=23.56
x*=100
iny y=(int)x
then use %

No. double x=23.56
x*=100
iny y=(int)x
then use %

I am trying to figure how the code works, but I still don't get it since I am beginner in programming.

Did you mean?

double x = 46.63; That's the value that was given.
x = 100;

int y = (int)x; then use %.

I've been trying numerous time to covert int to double, I always get an error in the code.

ok x=46.63
x = 100 * x // same as x*=100 ie x now equals 4663
google casting in c++
then work on ints using modulus

edit: the number i used was just random

double x = 46.62;
int cents = (int)x * 100;
int twenties = cents/(20*100);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.