943,572 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5689
  • C++ RSS
Nov 13th, 2008
0

Round Up Round down(truncate)

Expand Post »
Anyone have any idea how I would write a program that lets user input a floating point number and then either round up if number is like 4.5 our round down(truncate) if number like 4.49. Any help would be greatly appreciated.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
csaund1 is offline Offline
6 posts
since Sep 2008
Nov 13th, 2008
0

Re: Round Up Round down(truncate)

There are several ways that can be done. One way is to add 0.5 (or a similar value depending on where you want to round) to the original value and then cast it to an int and then back again if you want to round to the nearest decimal value place holder.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Nov 14th, 2008
0

Re: Round Up Round down(truncate)

can you show me some code
Reputation Points: 10
Solved Threads: 0
Newbie Poster
csaund1 is offline Offline
6 posts
since Sep 2008
Nov 14th, 2008
0

Re: Round Up Round down(truncate)

WARNING:Code based on my logic. Not compiled and tested in reality.

//round to closest integer.
double d = 2.49;
int i = (int)(d + 0.05);
cout << i << endl;

d = 2.51;
i = (int)(d + 0.05);
cout << i << endl;

//round to closest tenth (on first decimal point)
d = 2.41;
double temp = d * 10;
i = (int)(temp + 0.5);
temp = (double)(i/10);
cout << temp << endl;

d = 2.47;
temp = d * 10;
i = (int)(temp + 0.5);
temp = (double)(i/10);
cout << temp << endl;
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Nov 14th, 2008
0

Re: Round Up Round down(truncate)

YOU show us some code.
Consider a proper using of floor() library function.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 14th, 2008
0

Re: Round Up Round down(truncate)

Lerner, check up your code again. It's wrong ...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 14th, 2008
0

Re: Round Up Round down(truncate)

double RoundDouble(double doValue, int nPrecision)
{
static const double doBase = 10.0;
double doComplete5, doComplete5i;

doComplete5 = doValue * pow(doBase, (double) (nPrecision + 1));

if(doValue < 0.0)
doComplete5 -= 5.0;
else
doComplete5 += 5.0;

doComplete5 /= doBase;
modf(doComplete5, &doComplete5i);

return doComplete5i / pow(doBase, (double) nPrecision);
}
Reputation Points: 10
Solved Threads: 2
Newbie Poster
galin is offline Offline
10 posts
since Aug 2007
Nov 14th, 2008
0

Re: Round Up Round down(truncate)

>>Lerner, check up your code again. It's wrong

Yup; looks like this,

i = (int)(d + 0.05);

should be this:

i = (int)(d + 0.5);

Any other (il)logic errors (and there may be some) will need to wait for me to check it with a compiler. The theory remains intact even if I stub my toe, though. Thanks for pointing out my error!

There are certainly other ways to round and I suspect that using any of the other approaches already suggested and those yet to be suggested will also work.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Nov 14th, 2008
0

Re: Round Up Round down(truncate)

Use modf() function to splt integer and fractional parts of the double value before other operations (change a sign of negative value before other stuff then restore right sign after rounding). You lost some precision and may catch overflow with direct multiplication. Have a look to addition of 0.5 in Lerner's post (no need in if-else).
Using (twice) of relatively slow pow function is not the best idea. Consider another approach(es)...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 14th, 2008
0

Re: Round Up Round down(truncate)

That's incorrect: temp = (double)(i/10); . Integer division...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Linked List: Search and Modify Help
Next Thread in C++ Forum Timeline: having trouble with if statement and string





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC