Rounding numbers up...

This is a discussion on Rounding numbers up... within the C++ Programming forums, part of the General Programming Boards category; How can I round floats up to any number of decimal points? For instance.. if I have 34.52348.. I could ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    18

    Rounding numbers up...

    How can I round floats up to any number of decimal points?
    For instance.. if I have 34.52348.. I could call the function, something like round(24.52348,2) to round it up to 24.52... Maybe there's a function I don't know about... please help.
    ~void

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    It could be done with a function if you put the float into a char array, go through each one until you find the decimal, then go however many you need after the decimal and round up or down depending on the last digit.

    I don't know of any standard C++ function to do it, but a function like I described above wouldn't be hard to write.

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    #include <cmath>

    // ...

    const float pi = 3.14159;
    int piIsExactly3 = std::ceil(pi);

    In short, use std::ceil() found in <cmath>

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    To round to a whole number, either subtract .5 and use ceil() or add .5 and use floor().
    e.g., 1.4 + .5 = 1.9, floor(1.9) = 1 // actually 1.0
    1.6 + .5 = 2.1, floor (2.1) = 2
    Truth is a malleable commodity - Dick Cheney

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    ...and to round to n decimal places, multiply by 10^n, round, then divide by 10^n
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    559

    Re: Rounding numbers up...

    Originally posted by void
    How can I round floats up to any number of decimal points?
    For instance.. if I have 34.52348.. I could call the function, something like round(24.52348,2) to round it up to 24.52... Maybe there's a function I don't know about... please help.
    Actually, it's kind of tough to round 34.52348 to 24.52
    Using my previous example, you could add .005 and then use floor(). The number of zeros before the five is the number of decimal places you want to round to. Possibly the binary inexactness of floating point numbers will give errors with decimal places, not sure there.
    Truth is a malleable commodity - Dick Cheney

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Newbie question: rounding numbers
    By cantore in forum C Programming
    Replies: 10
    Last Post: 02-04-2006, 02:24 PM
  3. (C++) How would you go about rounding numbers?
    By jeffcoulter in forum C++ Programming
    Replies: 5
    Last Post: 09-19-2005, 07:47 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 10:15 AM
  5. Rounding Numbers accurately
    By crystaldawn68 in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2001, 02:23 PM

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