Help with Rounding 3 decimal places

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2009
Posts: 56
Reputation: songweaver is an unknown quantity at this point 
Solved Threads: 0
songweaver songweaver is offline Offline
Junior Poster in Training

Help with Rounding 3 decimal places

 
0
  #1
Apr 2nd, 2009
Hey guys, this function is for rounding 2 decimal places, e.g. 1.2785 rounds to 1.2800. First, how would I alter the function so it would round 3 decimal places 1.2780, and second how would I truncate it so it would read 1.278.

Thanks!

  1. double roundIt(double x, double n) //Write definition for rounding function
  2. {
  3. x = floor( x * pow(10.0, n) + 0.5) / pow(10.0, n);
  4. return x;
  5. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,699
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: 272
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Help with Rounding 3 decimal places

 
1
  #2
Apr 2nd, 2009
play around with the value of n and see what happens. In particular, try changing n from 1 to 2 to 3 etc. The function as written isn't specific for rounding to 2 decimal places.

Also, look into the stream manipulators, and here I'm thinking of the fixed and setprecision manipulators, to determine how many decimal points to display.
Last edited by Lerner; Apr 2nd, 2009 at 4:11 pm.
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: Help with Rounding 3 decimal places

 
0
  #3
Apr 2nd, 2009
Take into account that it's impossible to round decimal fractions accurately with binary floating point data
Try to avoid pow function using: it's the most ineffective and inaccurate method in that case.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 211
Reputation: rahul8590 is on a distinguished road 
Solved Threads: 11
rahul8590's Avatar
rahul8590 rahul8590 is offline Offline
Posting Whiz in Training

Re: Help with Rounding 3 decimal places

 
1
  #4
Apr 2nd, 2009
well at the first place i would recommend usage of float data type instead on double , guess float would do the trick.
second of all subtract the left part of the no from the original no itself , by doin this u can isolate the decimal part and then if u r looking for a 3 decimal round off in all the nos ( which apparently is ) .
u can multiply the 3 digits decimal into 100 and then round of them

  1.  
  2. float round( float no )
  3. {
  4. int x, temp;
  5. float y;
  6. temp = x = no ; only the integer part is taken
  7. y = no - x ; u get the decimal number here
  8. y*=100 ; decimal gets converted to 2 digit no n 1 decimal
  9. x = y;
  10. y-= x; the last decimal is stored in y
  11. if( y > = 0.5 )
  12. x++ ;
  13. return (temp + x/100) ;
  14. }

well i would like you to dry run the code .
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: Help with Rounding 3 decimal places

 
1
  #5
Apr 2nd, 2009
NEVER use float data type in math and financial calculations, it has too low precision. Standard C and C++ floating point type is double (have a look at <cmath> header contents).

Use modf library function to separate integer and fractional parts.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 56
Reputation: songweaver is an unknown quantity at this point 
Solved Threads: 0
songweaver songweaver is offline Offline
Junior Poster in Training

Re: Help with Rounding 3 decimal places

 
0
  #6
Apr 3rd, 2009
thanks guys! I am trying to incorporate this in a weird kinda of assignment, I may need to get with you later. I will go ahead mark as solved. As always I throw myself at your wisdom!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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