# Rounding decimals

• 07-31-2011
brewgrass
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?
• 07-31-2011
GReaper
Quote:

Originally Posted by brewgrass
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.
• 07-31-2011
brewgrass
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
• 07-31-2011
kmdv
Quote:

Originally Posted by brewgrass
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.
• 07-31-2011
tabstop
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.
• 07-31-2011
iMalc
Quote:

Originally Posted by kmdv
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.
• 08-01-2011
brewgrass
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.