# Thread: Rounding a floating point #

1. ## Rounding a floating point #

I have tried to round the float point number to a whole number in 2 different ways:
I am trying to round up on the cans of paint. I have tried by using cans = (int)(surface + 0.5)/300; I still get the answer 0 cans even though the initial value of the surface variable is .61. I then tried cans = (int)(surface * 10)/300; which gave me 6 cans. Is there a round function with C that works like the round function in a sql statement?
#include <stdio.h>

int main()
{
float width; /* integer to hold the width value */
float length; /* integer to hold the length value */
float area; /* integer to hold the area value */
float yard; /* integer to hold the yard value */
float height; /* integer to hold the height value */
float surface; /* integer to hold the surface value */
int cans; /* integer to hold the cans of paint value */

printf("Please enter the width of the room:\n"); /* ask for the width */
scanf("%f", &width); /* intake the width */

printf("Please enter the length of the room:\n"); /* ask for the length */
scanf("%f", &length); /* intake the length */

printf("Please enter the height of the walls\n"); /* ask for the height */
scanf("%f", &height); /* intake the height */

area = width + length; /* variable of area w + l gives the value */

yard = area /3; /* variable of yard is w + l /3 the value */

surface = (height + width + length) *4; /* variable of surface is h + w + l *4 walls = the value */

cans = (int)(surface * 10)/300; /* variable of cans is h + w + l *4/300sq.ft= the value */

printf("You will need %f square feet of carpet\n", area); /* output the area to screen */

printf("You will need %f square yards of carpet\n", yard); /* output the yards to screen */

printf("The walls are %f square feet\n", surface); /* output the surface to screen */

printf("You will need %d cans of paint\n", cans); /* output the cans to screen */
scanf("Enter anything:\n",&cans); /* a pause to be commented out */
return 0;
}

2. #include <math.h>
double ceil(double x);

Here is an example.
Code:
```#include <stdio.h>
#include <math.h>

int main(void)
{
double surface = 0.61;
int cans = (int)ceil(surface / 300);
printf("cans = %d\n", cans);
return 0;
}

/* my output
cans = 1
*/```

3. I will try that.
Thank you,
John

4. Basically floats and doubles take superiority over int so if you want to to round the number in an int format then place the calculation in a variable that is of int.
Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{
float gal,oz;
printf("Enter ounces of gas: ");
scanf("%.1f",&oz);

gal = (oz/128);
printf("\nThere are %i gallons in %.1f ounces.\n",(int)gal,oz);
return 0;
}```

5. Originally posted by _Cl0wn_
Basically floats and doubles take superiority over int so if you want to to round the number in an int format then place the calculation in a variable that is of int.
• When a finite value of real floating type is converted to an integer, the fractional part is discarded (i.e., the value is truncated toward zero).
john_murphy69 wanted to round up; i.e., 0.5 becomes 1. The integer conversion does not do this. ceil will, but its return value is a double. Performing the integer conversion on the value returned by ceil will then produce the result john_murphy69 was trying to obtain.
Originally posted by _Cl0wn_
Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{
float gal,oz;
printf("Enter ounces of gas: ");
scanf("%.1f",&oz);

gal = (oz/128);
printf("\nThere are %i gallons in %.1f ounces.\n",(int)gal,oz);
return 0;
}```
Unlike printf, scanf does not have a precision specifier.