# Thread: I think I have a glich in my program using integer and double values

1. ## I think I have a glich in my program using integer and double values

Ok basically what I want to do is turn say \$3.35 all in pennies whic will be 335. My problem is how do I stored a floating point value 3.335 like so into an int value so I can have 334 not 333.

here is my code I think it works sometimes but not all of them. what i think I did was copy the value of a float into an int to two values after the decimal point and then multiply it by 100. I noticed that some times will not match my float value by one cent.

Code:
```
float unitp = 1.25;
float want;
float totalamount;
float  payment;
float change;
int dchange;
int newchange;

printf("Welcome to crocop's meat market\n");
printf("the meat is priced at \$1.25 each pound\n");
printf("How many pounds would you like to buy?\n");
scanf("%f", &want);

totalamount=want * unitp;

printf("the total you owe is \$%.2f\n", totalamount);

printf("How much do you want to make the payment for?\n");
scanf("%f", &payment);

change = payment-totalamount ;

printf("okay you will recive \$%.2f in change\n", change);

dchange=( 100* change+.5);

printf("dchange = %d\n", dchange);```
I want the lines highlighted in red to alwasy print the same values. Thank you for your help.

2. I haven't tested it, but you might want to try:
Code:
`dchange = (int)round(100 * change);`
Note: You might have to #include <math.h>, and possibly link in the math library if you're using gcc (add "-lm" to the compile line)

The bottom line is, floating point multiplication is goofy and fraught with rounding errors like this.

3. Originally Posted by itsme86
I haven't tested it, but you might want to try:
Code:
`dchange = (int)round(100 * change);`
Note: You might have to #include <math.h>, and possibly link in the math library if you're using gcc (add "-lm" to the compile line)

The bottom line is, floating point multiplication is goofy and fraught with rounding errors like this.
yeah I tried it and gives me some weird values. thanks for the help though.

ok what I did now is change it to

dchange=( 100* change+.4); so now is .4 not .5 and it seems to be working and I don't know why.

4. Do you have an example of failing values? I copied and pasted it into visual studio and cannot get it to fail even when the change due back is 3.335. (I get 3.34 and 334).

I think visual studio automatically uses a double when you want a float, maybe that is what you want?

5. Originally Posted by dolfaniss
Do you have an example of failing values? I copied and pasted it into visual studio and cannot get it to fail even when the change due back is 3.335. (I get 3.34 and 334).

I think visual studio automatically uses a double when you want a float, maybe that is what you want?
ok try to to buy 3.5 pounds and then pay with 10. I am using putty's gcc to compile it.

6. I get 5.63 and 563. The only time I got a mistake is when I accidently entered 3 lbs (for 3.75) and tried to pay 3.335 for it. In that case I got -41 and -40 otherwise I can't get a failure.

7. i guess is gcc compiler then.

8. Could be, I'm not sure maybe some of the more savy members here would know. I'm actually in a C class that seems to be pretty cose to in line with where you are right now. I'm a beginner myself who likes to try to help out here for practice, but I'm mostly here to learn. From my experience being here nobody would have a problem telling me my code sucks if they see something wrong, LOL.

9. Can you restate the problem? What number is the calculation failing on?

10. Originally Posted by dolfaniss
Do you have an example of failing values? I copied and pasted it into visual studio and cannot get it to fail even when the change due back is 3.335. (I get 3.34 and 334).

I think visual studio automatically uses a double when you want a float, maybe that is what you want?
For your explaination: Suppose i do
Code:
`float x=2.24;`
2.24 is actually treated as double but implicit type cast will convert it into float.
So if you want to keep it float,
Code:
`float x=2.24f;`

11. Originally Posted by Mr.777
For your explaination: Suppose i do
Code:
`float x=2.24;`
2.24 is actually treated as double but implicit type cast will convert it into float.
So if you want to keep it float,
Code:
`float x=2.24f;`
Thanks for clearing that up for me!