rounding

This is a discussion on rounding within the C++ Programming forums, part of the General Programming Boards category; Hi, I was wondering if anyone knew of a function that could do rounding to the nearest whole number? e.g ...

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

    rounding

    Hi,
    I was wondering if anyone knew of a function that could do rounding to the nearest whole number?

    e.g 1.32 -> 1
    1.53->2

    Thanks

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    There isn't one that I know of, but its easy to write one yourself:
    Code:
    int round(float f)
    {
        if (int(f-.5)==int(f))
              return int(f)+1;
        else
              return int(f);
    }

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    20
    okay, thx =o)

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    floor() and ceil() in <math.h>; rather
    std::floor() and std::ceil() in <cmath>
    Last edited by Eibro; 02-11-2003 at 02:19 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rounding off a double
    By C-Dummy in forum C Programming
    Replies: 3
    Last Post: 06-23-2008, 11:45 AM
  2. setprecision() - can I count on it rounding or not?
    By major_small in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2005, 01:26 PM
  3. Rounding errors
    By Buckshot in forum C++ Programming
    Replies: 15
    Last Post: 08-16-2005, 09:11 PM
  4. preventing rounding problems with doubles
    By mccoz in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2004, 08:23 AM
  5. Help with rounding a number
    By nickk in forum C Programming
    Replies: 3
    Last Post: 06-02-2004, 11:44 AM

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