![]() |
|
|
|||||||
![]() |
|
|
Thread Tools | Rate Thread |
|
|
PM User | #1 |
|
New to the CF scene Join Date: Nov 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
rounding whole numbers in C++
I am ABSOLUTELY brand new to C++ and have a question about rounding numbers.
What I need to do is make a program that determines whether or not a number is a multiple of 1000, and if not to automatically round the number up to the next highest multiple of 1000 (i.e. if the number is 579, then it would need to be rounded to 1000; if the number is 3568, it needs to be rounded to 4000, etc...). This needs to be done in a user-defined function and returned to the main. Here's what I have so far: #include <iostream> #include <cmath> #include <iomanip> using namespace std; double RoundingFunction (double nonrounded_number) { double rounded_number if (nonrounded_number % 1000 == 0) then return (nonrounded_number); else rounded_number = nonrounded_number + 1000; } int main () */ rest of program /* -------------------------------------------------------- I know adding 1000 won't do the trick, but I'm not sure what to use... is it the ceil(x) function? If so, what would that look like? |
|
|
|
|
|
PM User | #2 |
|
The Spaminator ![]() ![]() Join Date: Jun 2002
Location: USA
Posts: 7,286
Thanks: 1
Thanked 148 Times in 145 Posts
![]() ![]() |
Have it print out the result of
1000 - nonrounded_number % 1000 and you'll see what you need to add.
__________________
OracleGuy My Blog "... the VP of our third biggest account started sweating bullets in our latest project status conference when Roy stood up and asserted: 'We don’t code our products for SMACKTARDS.'" - Daily Victim #564 |
|
|
|
|
|
PM User | #3 | |
|
New to the CF scene Join Date: Nov 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
Quote:
#include <cmath> #include <iomanip> using namespace std; double RoundingFunction (double nonrounded_number) { return ceil(nonrounded_number/1000)*1000; } int main () */ rest of program /* |
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Rate This Thread | |
|
|