- #1
- 4,652
- 37
I've been frustrated for a couple of days over a C programming assignment:
"Convert the float value tot_bill (in dollars and cents) to integer dollars in tot_doll and integer cents in tot_cents. Make sure that the cents value is rounded, not truncated."
I am assuming that we should take a value like 12.37 cents and store the values in two integer variables. The tot_doll value should contain the value 12. That's easy enough to do - conversion to an integer will take care of that.
The tot_cents should contain a value of 37. That's not so bad unless instead of 12.37, I have a value of 12.378.
How do I get the rounded value from a float (.378) into an integer variable so I can store 38 for the integer tot_cents?
I thought about storing 378 as an integer value and then using a conversion modifier to convert on the fly to a value of 38 when I print, but I don't know if that's what's expected (or if it's even possible).
I am probably making this a lot harder than it is.
Thanks!
"Convert the float value tot_bill (in dollars and cents) to integer dollars in tot_doll and integer cents in tot_cents. Make sure that the cents value is rounded, not truncated."
I am assuming that we should take a value like 12.37 cents and store the values in two integer variables. The tot_doll value should contain the value 12. That's easy enough to do - conversion to an integer will take care of that.
The tot_cents should contain a value of 37. That's not so bad unless instead of 12.37, I have a value of 12.378.
How do I get the rounded value from a float (.378) into an integer variable so I can store 38 for the integer tot_cents?
I thought about storing 378 as an integer value and then using a conversion modifier to convert on the fly to a value of 38 when I print, but I don't know if that's what's expected (or if it's even possible).
I am probably making this a lot harder than it is.
Thanks!