# Thread: meters to feet inches (rounding inches)

1. ## meters to feet inches (rounding inches)

Whats up, I got a small problem with an assignment: so I am supposed to convert feet and inches to meters; however, the function i must use must have feet and inches as (int) variables. by doing this the inch units will not be rounded....

any idea how can i fix this using "int"?

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

void meters_to_feet_inches(double m, int *ft, int *in);
int main()
{
int feet, inches;
double meters;

printf("enter meters to be converted\n");
scanf("%lf", &meters);
meters_to_feet_inches(meters, &feet, &inches);
printf("meters in inches is = %d feet, %d inches\n", feet, inches);

return 0;
}

void meters_to_feet_inches(double m, int *ft, int *in)
{
double inft, temp;
inft=m*39.37;
*ft=inft/12;
*in=(int)inft%12;
}

2. To round off a double:
set your original translated unit to temp (as a double, still). Now add 0.5 to it, and continue.

When that number is cast back to an int, it will be rounded off.

BTW, you're supposed to round from a meter to feet and inches, not the other way around, according to your program code.

3. Thank you, it seems to work now though i dont know how the 0.5 makes it work

4. Integer math truncates (cuts off) the part of a number less than one.

So if you had 1.4 and added 0.5 you'd have 1.9, and after int math, that would be truncated to 1. so it rounds correctly.

If the value was 1.5 instead, then 0.5 added to it, bring it to 2.0, which is then truncated by int math to just 2.

So again, it's rounded just the right way.

I don't know whether your program needed to round off the inches or not, because it wasn't explained, but that's what you asked about - so you go it, and it's a good trick to remember.