Thread: rounding up a double.

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    36

    rounding up a double.

    Hey again,

    is there anyway I can round up a double, like:

    2367.4242 to a long 2367

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Yes. There is a search feature for this forum.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    Ok, Thanks for pointing that out.
    I noticed there was supposed to be a round function in the math lib, but I don't appear to have it.
    I looked at another few past posts but I am still a bit confused , and actually I think I just want to trunicate it...like

    double num[i] = 2345.565656 to become
    long temp[i] =2345

    sorry for all the bother, but my heads not thinking clear with this head cold I have

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Code:
    temp[i] = num[i];

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    lol thanks very much so simple!
    helped me figure out something harder tho

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    C will automatically round down, or simply cut off the decimal, if you assign a floating point value to an integer variable. If you wish to round up all the time, simply do that and then add one to the integer. If you wanted to round up or down depending on the value of the floating point variable, you would have to do a slightly more complex system of checking to see if the decimal point is higher or lower than .5

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Or you could just add .5:
    Code:
    double d = 1.6, e = 1.3;
    int x = (int)(d+.5), y = (int)(e+.5);
    /* x = 2, y = 1 */
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM