943,568 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 9015
  • C++ RSS
Apr 2nd, 2009
0

Help with Rounding 3 decimal places

Expand Post »
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!

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
songweaver is offline Offline
80 posts
since Mar 2009
Apr 2nd, 2009
1

Re: Help with Rounding 3 decimal places

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Apr 2nd, 2009
0

Re: Help with Rounding 3 decimal places

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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Apr 2nd, 2009
1

Re: Help with Rounding 3 decimal places

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

C++ Syntax (Toggle Plain Text)
  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 .
Reputation Points: 92
Solved Threads: 20
Posting Whiz
rahul8590 is offline Offline
351 posts
since Mar 2009
Apr 2nd, 2009
1

Re: Help with Rounding 3 decimal places

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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Apr 3rd, 2009
0

Re: Help with Rounding 3 decimal places

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!
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
songweaver is offline Offline
80 posts
since Mar 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: VC++ programmatically scroll down in listview
Next Thread in C++ Forum Timeline: C++ Program Hangs





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


Follow us on Twitter


© 2011 DaniWeb® LLC