C program: storing rounded float to int variable

Click For Summary

Homework Help Overview

The discussion revolves around a C programming assignment that requires converting a float value representing a total bill into two integer variables: one for dollars and another for rounded cents. Participants are exploring how to correctly round the cents value rather than truncate it.

Discussion Character

  • Exploratory, Conceptual clarification, Mathematical reasoning

Approaches and Questions Raised

  • The original poster attempts to clarify how to round the cents value derived from a float. They express uncertainty about the expected method for rounding and whether storing intermediate values as integers is appropriate. Other participants suggest using specific functions from libraries, such as round() from stdlib.h and math.h, to achieve the desired rounding behavior.

Discussion Status

Some participants have provided guidance on using the round() function and shared code snippets demonstrating their approaches. The original poster has shared their implementation and output, indicating progress in their understanding, though questions about the differences between rounding functions remain open.

Contextual Notes

The discussion includes considerations about the rounding method and the potential differences between functions in different libraries, as well as the implications of truncating versus rounding values in programming.

Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
Messages
4,663
Reaction score
36
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
#include <stdlib.h> and use the round() function:

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

- Warren
 
Last edited by a moderator:
Thanks, chroot!
 
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;
}
 
and the output is...

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

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
5
Views
5K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • Sticky
  • · Replies 1 ·
Replies
1
Views
17K
  • · Replies 4 ·
Replies
4
Views
5K