Thread: Rounding float problem.....

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    21

    Rounding float problem.....

    Hi,

    Can somebody advise what the best method is to round a float value UP to the nearest whole value:

    i.e 1.678 should result in 2.000
    i.e 1.012 should also result in 2.000
    i.e 1.001 should result in 1.000

    Thanks,

    Manny

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    4

    ceil

    ceil in math.h does what you want:

    http://www.cplusplus.com/reference/c...math/ceil.html

    osuee

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    40
    ceil normally would do the job, but it will not for turning 1.001 into 1.000, because that is not rounded up, that is either rounded off or rounded down. Perhaps that was a typo.

  4. #4
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Try this on for size:
    Code:
    float num;
    float rounded_num;
    
    printf("Enter the number you want rounded: ");
    scanf("%f", &num);
    rounded_num = (int)(num + .5f);
    
    printf("%f rounded to the nearest whole value is %f\n", num, rounded_num);
    Don't quote me on that... ...seriously

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Brad0407 View Post

    Code:
    rounded_num = (int)(num + .5f);
    This is incorrect if num is negative. For instance, (int)(-0.9 + 0.5) == (int)(-0.4) == 0. But -0.9 is obviously closer to -1, not 0.

    The answer is to add 0.5 if num is positive, -0.5 if num is negative:

    Code:
    rounded_num = (int)(num + (num < 0) ? -0.5 : 0.5);

  6. #6
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Quote Originally Posted by brewbuck View Post
    This is incorrect if num is negative. For instance, (int)(-0.9 + 0.5) == (int)(-0.4) == 0. But -0.9 is obviously closer to -1, not 0.

    The answer is to add 0.5 if num is positive, -0.5 if num is negative:

    Code:
    rounded_num = (int)(num + (num < 0) ? -0.5 : 0.5);
    You mean:
    Code:
    rounded_num = (int)(num + (num < 0 ? -0.5 : 0.5));
    Don't quote me on that... ...seriously

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Brad0407 View Post
    You mean:
    Code:
    rounded_num = (int)(num + (num < 0 ? -0.5 : 0.5));
    You're right. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  3. help me
    By warthog89 in forum C Programming
    Replies: 11
    Last Post: 09-30-2006, 08:17 AM
  4. Integration problem
    By HAssan in forum C Programming
    Replies: 4
    Last Post: 01-23-2006, 03:07 AM
  5. error in program????
    By SpEkTrE in forum C Programming
    Replies: 5
    Last Post: 11-24-2003, 06:16 PM