Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 11-07-2009, 06:17 PM   PM User | #1
drumminfool91
New to the CF scene

 
Join Date: Nov 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
drumminfool91 is an unknown quantity at this point
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?
drumminfool91 is offline   Reply With Quote
Old 11-08-2009, 01:09 AM   PM User | #2
oracleguy
The Spaminator


 
Join Date: Jun 2002
Location: USA
Posts: 7,286
Thanks: 1
Thanked 148 Times in 145 Posts
oracleguy has a spectacular aura aboutoracleguy has a spectacular aura about
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
oracleguy is offline   Reply With Quote
Old 11-09-2009, 12:56 PM   PM User | #3
gtguy87
New to the CF scene

 
Join Date: Nov 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
gtguy87 is an unknown quantity at this point
Quote:
Originally Posted by drumminfool91 View Post
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?
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

double RoundingFunction (double nonrounded_number)
{
return ceil(nonrounded_number/1000)*1000;
}

int main ()
*/ rest of program /*
gtguy87 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:35 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.