C program: storing rounded float to int variable

In summary, the conversation is about converting a float value representing a monetary amount into integer values for dollars and cents. The challenge is rounding the cents value correctly, especially in cases where the float has more decimal places. The solution involves using the round() function from the stdlib.h library and some additional steps to ensure the rounding is accurate.
  • #1
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
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!
 
Physics news on Phys.org
  • #2
#include <stdlib.h> and use the round() function:

http://www.sciface.com/STATIC/DOC30/eng/stdlib_round.html

- Warren
 
Last edited by a moderator:
  • #3
Thanks, chroot!
 
  • #4
here's what I ended up doing

I'm sure it could have been done in less steps I think but this seemed to get the job done. I am not quite sure how the rounding function differs between math.h and stdlib.h or if they are essentially the same. My teacher had suggested using math.h for rounding, so I gave that a try.

#include <stdio.h>
#include <math.h>

int main(void)
{
float tot_bill = 35.6777;
int tot_doll = tot_bill;
// just stores the dollars as integer value - truncates
float leftover = (tot_bill - tot_doll)*100;
//gets cents leftover, moves decimal place 2 to right
int tot_cents = ceil(leftover);
//rounds up the xx.xxx decimal made in last step
//to next closest integer, stores as int.

printf("This is the unrounded bill: %f \n", tot_bill);
printf("These are the total dollars: %d \n", tot_doll);
printf("These are the rounded total cents: %d \n", tot_cents);

return 0;
}
 
  • #5
and the output is...

This is the unrounded bill: 35.677700
These are the total dollars: 35
These are the rounded total cents: 68
 

1. What is the purpose of storing a rounded float to an int variable in C?

The purpose of storing a rounded float to an int variable in C is to convert a decimal value to a whole number. This is useful when performing calculations that require whole numbers, such as counting or indexing.

2. How do you store a rounded float to an int variable in C?

To store a rounded float to an int variable in C, you can use the round() function from the math library. This function takes in a float value and returns the closest integer value. You can then assign this value to your int variable.

3. What happens if a float value is too large to be stored in an int variable?

If a float value is too large to be stored in an int variable, it will result in an overflow. This means that the value will be truncated and may result in unexpected or erroneous calculations. It is important to ensure that the float value is within the range of the int variable before rounding and storing it.

4. Can you store a rounded float to a char variable instead of an int variable?

Yes, you can store a rounded float to a char variable instead of an int variable. However, the resulting value will be truncated to fit into the smaller data type. This may result in a loss of precision and should only be done if the float value is within the range of the char data type.

5. Is it possible to store a rounded float to an int variable without using the round() function?

Yes, it is possible to store a rounded float to an int variable without using the round() function. Another method is to use type casting, where you can explicitly convert the float value to an int data type. This will truncate the decimal value and only store the whole number portion of the float value.

Similar threads

  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
8
Views
878
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
977
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Programming and Computer Science
Replies
22
Views
921
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
2K
Back
Top