rounding off floats

This is a discussion on rounding off floats within the C Programming forums, part of the General Programming Boards category; hello here is the problem.I want to round off a float as follow 1.2 to 1 1.5 to 1 or ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    12

    rounding off floats

    hello

    here is the problem.I want to round off a float as follow

    1.2 to 1

    1.5 to 1 or 2

    1.7 to 2


    could any suggest what function to use. I am writing code in
    C using Microsoft visual studio 2005

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    C has a function called round.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    12
    the round function does not work in visual studio 2005. I tried and got a error message.
    thanks

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So round is somewhat new, yes; you should certainly have floor in math.h, which returns the next lowest integer, so you'll have to massage the input first.

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Adding 0.5 and getting the lowest integer should do the trick, right?

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by twomers View Post
    Adding 0.5 and getting the lowest integer should do the trick, right?
    Yep, that's the best way if you only have truncation available to you. Be aware though that if you have a floating point inaccuracy, you might get undesired results.
    e.g. if you have 6.4999999999999999 and you add 0.5, you get 6.99999999999999, which when floored is 6 not 7.
    One way to get around this is to add an epsilon value. Take the smallest number of decimal places you care about (say 5) add two more decimal places and then add 10^-x to your number:
    e.g. if you have 6.4999999999 + 0.0000001 = 6.5 6.5 + 0.5 = 7

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rounding off floats
    By creeping death in forum C Programming
    Replies: 6
    Last Post: 04-01-2009, 01:49 PM
  2. Reading errors in floats
    By Improvolone in forum C++ Programming
    Replies: 8
    Last Post: 03-21-2006, 02:20 PM
  3. Help with rounding a number
    By nickk in forum C Programming
    Replies: 3
    Last Post: 06-02-2004, 11:44 AM
  4. comparing two arrays of floats
    By COBOL2C++ in forum C++ Programming
    Replies: 7
    Last Post: 07-16-2003, 03:22 AM
  5. Rounding Floats
    By Beginner921 in forum C Programming
    Replies: 5
    Last Post: 02-23-2003, 11:02 AM

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