943,754 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 15235
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 13th, 2006
0

round off number

Expand Post »
how will i round off the return value to decimal place with tis function?

  1. unsigned int CalculatePercentage(int TotalComponent, int PartialComponent){
  2. return int((PartialComponent/TotalComponent)100);
  3. }
Similar Threads
Reputation Points: 16
Solved Threads: 0
Light Poster
vbcielle is offline Offline
26 posts
since Sep 2006
Oct 13th, 2006
0

Re: round off number

Click to Expand / Collapse  Quote originally posted by vbcielle ...
how will i round off the return value to decimal place with tis function?

  1. unsigned int CalculatePercentage(int TotalComponent, int PartialComponent){
  2. return int((PartialComponent/TotalComponent)100);
  3. }
You must multiply with 100 (for multiplaying use *).
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Oct 13th, 2006
0

Re: round off number

Click to Expand / Collapse  Quote originally posted by andor ...
You must multiply with 100 (for multiplaying use *).
okie thanks but how can i round it off to one deciamal place?

for example

(PartialComponent/TotalComponent)*100
90/235=38.3 %

38.3 should be the return value

anyone? i shouldnt be using any math.h function etc.
Reputation Points: 16
Solved Threads: 0
Light Poster
vbcielle is offline Offline
26 posts
since Sep 2006
Oct 13th, 2006
0

Re: round off number

Try:
floatValue = float(int(floatValue *  10 + 0.5)) / 10;
Reputation Points: 22
Solved Threads: 11
Posting Whiz in Training
DavidRyan is offline Offline
229 posts
since Jul 2006
Oct 13th, 2006
0

Re: round off number

Click to Expand / Collapse  Quote originally posted by vbcielle ...
okie thanks but how can i round it off to one deciamal place?

for example

(PartialComponent/TotalComponent)*100
90/235=38.3 %

38.3 should be the return value

anyone? i shouldnt be using any math.h function etc.
Your func CalculatePercentage should return double or float instead of unsigned int, and to round read this
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Oct 13th, 2006
0

Re: round off number

The rounding is done in your display function. Here is C example:
  1. // percent calculation, wants 100 * 90/235 --> 38.3 %
  2.  
  3. #include <stdio.h>
  4.  
  5. double CalculatePercentage(double TotalComponent, double PartialComponent)
  6. {
  7. return 100*PartialComponent/TotalComponent;
  8. }
  9.  
  10. int main()
  11. {
  12. printf("100 * 90/235 --> %0.1f%c", CalculatePercentage(235, 90), '%');
  13.  
  14. getchar(); // wait for key press
  15. return 0;
  16. }
And here is C++ example:
  1. // percent calculation, wants 100 * 90/235 --> 38.3 %
  2.  
  3. #include <iostream>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. double CalculatePercentage(double TotalComponent, double PartialComponent)
  9. {
  10. return 100*PartialComponent/TotalComponent;
  11. }
  12.  
  13. int main()
  14. {
  15. cout.setf(ios::fixed);
  16. cout<< setprecision(1) << "100 * 90/235 --> " << CalculatePercentage(235, 90) << '%' << endl;
  17.  
  18. cin.get(); // wait for key press
  19. return 0;
  20. }
Last edited by bumsfeld; Oct 13th, 2006 at 4:31 pm.
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Oct 13th, 2006
0

Re: round off number

The question is about rounding off the returned value and not rounding off while displaying. Read the first post.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Oct 13th, 2006
0

Re: round off number

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
The question is about rounding off the returned value and not rounding off while displaying. Read the first post.
This does not make any sense, if the return value is double or float you can not guarantee precision to be 38.3 it most like will be 38.2999999 or something like that.
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Oct 13th, 2006
0

Re: round off number

Try out something like this, though its a bit round about way, but should work:
  1. double return_round( double value, double total )
  2. {
  3. char buffer[BUFSIZ] = {'\0'} ;
  4. double percentage = ( value / total ) * 100 ;
  5. printf( "\nActual number: %lf", percentage ) ;
  6.  
  7. sprintf(buffer, "%0.2f", percentage) ;
  8.  
  9. if ( buffer[ strlen( buffer ) - 1 ] >= '5' )
  10. buffer[ strlen( buffer ) - 2 ] += 1 ;
  11. buffer[ strlen( buffer ) - 1 ] = '\0' ;
  12.  
  13. printf( "\nThe rounded string is: %s", buffer ) ;
  14.  
  15. return atof( buffer ) ;
  16. }

Hope it helped, bye.
Last edited by ~s.o.s~; Oct 13th, 2006 at 5:16 pm.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Oct 15th, 2006
0

Re: round off number

I'm with bumsfeld on this one. You want as much precision as you can get throughout the program's calculations and only round for the display. You don't even want to round the number itself, just the display.

If you round prematurely, all your calculations will be slightly off. And the more calculations you do, the more inaccurate your answer.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006

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: aol instant messenger working???????
Next Thread in C Forum Timeline: How do I use a pointer to an array of structures...?





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


Follow us on Twitter


© 2011 DaniWeb® LLC