Thread: Rounding decimals

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    7

    Rounding decimals

    I am new to programming and chose C++ as a starting language. I am creating a program (for practice). I want the user to enter 5 numbers that are a combination of integers and decimals. How would I round decimals < .50 down to the nearest integer and decimals that are >= .50 to round up. I do not want to truncate because that will always round down. I think I need to declare variables a, b, c, d, e. Do I need an if/else statement to round up and down?
    Eventually I will add and divide the 5 integers to find the median, but I should be able to figure that out on my own.
    I apologize for such an amateur question but I just wrote my first programs yesterday.
    Is C++ a good language for a beginner?
    Thank you in advance.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by brewgrass View Post
    Do I need an if/else statement to round up and down?
    You don't *need* an if/else statement, it's just one way to do it.

    Think how C/C++ rounds floating-point numbers to integers: 4.1 ==> 4, 4.5 ==> 4, 4.999 ==> 4
    It rounds to the nearest integer that is *smaller* than the number!
    Think how you can take advantage of that to make the rounding you want.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    7
    Thank you both for your replies. Sipher, I understand that the truncate feature will round down for numbers less than .5. What is confusing me is how I need to declare the user-entered numbers so that I maintain the original number before rounding and can then remove the decimals and retain the integer or add 1 to the integer. As I mentioned I am very new to programming and have only read a few chapters of the book that I am learning from. However, I do live by the motto... Never let your lack of knowledge stop you from doing anything. Knowledge will come.
    Thanks

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by brewgrass View Post
    I am new to programming and chose C++ as a starting language. I am creating a program (for practice). I want the user to enter 5 numbers that are a combination of integers and decimals. How would I round decimals < .50 down to the nearest integer and decimals that are >= .50 to round up. I do not want to truncate because that will always round down.
    You probably want to do this:

    Code:
    double entered;
    cin >> entered;
    double rounded = (double)(long)(entered + 0.5);
    This may not work for very large numbers (like more than several billions), which cannot be represented by the 'long' integer.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Technically, round() appears in C99 only, not C89, so it is not guaranteed to be in C++ (unless they fixed that in C++11), but you may want to give it a try -- or better yet check your installation to see if its available.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by kmdv View Post
    You probably want to do this:

    Code:
    double entered;
    cin >> entered;
    double rounded = (double)(long)(entered + 0.5);
    This may not work for very large numbers (like more than several billions), which cannot be represented by the 'long' integer.
    Which is precisely why you may NOT want to do that. There is no need to involve slow conversions to and from an integer representation. Nor is there a need to involve a representation that will introduce error into large numbers. Nor is there a need to introduce unnecessary casts.

    What you actually want is just:
    Code:
    rounded = floor(entered + 0.5);
    This is the whole reason that a function like floor exists.
    Last edited by iMalc; 08-01-2011 at 12:01 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    7
    Thank you all for your help. I have a better understanding of what I need to do now. I will work on this program and see if I can get it operational. At least if I need help again, I can provide code to decipher. I love those AH HA moments when learning something new.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-24-2011, 02:28 PM
  2. allow decimals
    By dankassdann in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2006, 06:41 PM
  3. Help with Decimals
    By Deadlyaim in forum C++ Programming
    Replies: 8
    Last Post: 09-28-2006, 05:15 PM
  4. Getting enough decimals
    By Drogin in forum C++ Programming
    Replies: 3
    Last Post: 10-29-2005, 09:37 PM
  5. decimals
    By KJ_Magic in forum C++ Programming
    Replies: 13
    Last Post: 04-16-2003, 09:13 PM