Round Up Round down(truncate)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 6
Reputation: csaund1 is an unknown quantity at this point 
Solved Threads: 0
csaund1 csaund1 is offline Offline
Newbie Poster

Round Up Round down(truncate)

 
0
  #1
Nov 13th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,692
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 267
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Round Up Round down(truncate)

 
0
  #2
Nov 13th, 2008
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.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 6
Reputation: csaund1 is an unknown quantity at this point 
Solved Threads: 0
csaund1 csaund1 is offline Offline
Newbie Poster

Re: Round Up Round down(truncate)

 
0
  #3
Nov 14th, 2008
can you show me some code
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,692
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 267
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Round Up Round down(truncate)

 
0
  #4
Nov 14th, 2008
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;
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Round Up Round down(truncate)

 
0
  #5
Nov 14th, 2008
YOU show us some code.
Consider a proper using of floor() library function.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Round Up Round down(truncate)

 
0
  #6
Nov 14th, 2008
Lerner, check up your code again. It's wrong ...
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 10
Reputation: galin is an unknown quantity at this point 
Solved Threads: 2
galin galin is offline Offline
Newbie Poster

Re: Round Up Round down(truncate)

 
0
  #7
Nov 14th, 2008
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);
}
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,692
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 267
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Round Up Round down(truncate)

 
0
  #8
Nov 14th, 2008
>>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.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Round Up Round down(truncate)

 
0
  #9
Nov 14th, 2008
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)...
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Round Up Round down(truncate)

 
0
  #10
Nov 14th, 2008
That's incorrect: temp = (double)(i/10); . Integer division...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC