Rounding a floating point #

This is a discussion on Rounding a floating point # within the C Programming forums, part of the General Programming Boards category; I have tried to round the float point number to a whole number in 2 different ways: I am trying ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    42

    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?
    Please see my code to help you understand what I am doing.
    #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. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    #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. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    I will try that.
    Thank you,
    John

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    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. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 09:14 PM
  3. floating point question
    By Eric Cheong in forum C Programming
    Replies: 8
    Last Post: 09-10-2004, 11:48 PM
  4. 2 questions about floating point and %
    By ams80 in forum C Programming
    Replies: 2
    Last Post: 08-14-2002, 11:55 AM
  5. Replies: 2
    Last Post: 09-10-2001, 01:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21