round off number

Reply

Join Date: Sep 2006
Posts: 26
Reputation: vbcielle is an unknown quantity at this point 
Solved Threads: 0
vbcielle vbcielle is offline Offline
Light Poster

round off number

 
0
  #1
Oct 13th, 2006
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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: round off number

 
0
  #2
Oct 13th, 2006
Originally Posted by vbcielle View 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. }
You must multiply with 100 (for multiplaying use *).
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 26
Reputation: vbcielle is an unknown quantity at this point 
Solved Threads: 0
vbcielle vbcielle is offline Offline
Light Poster

Re: round off number

 
0
  #3
Oct 13th, 2006
Originally Posted by andor View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 229
Reputation: DavidRyan is an unknown quantity at this point 
Solved Threads: 11
DavidRyan's Avatar
DavidRyan DavidRyan is offline Offline
Posting Whiz in Training

Re: round off number

 
0
  #4
Oct 13th, 2006
Try:
floatValue = float(int(floatValue *  10 + 0.5)) / 10;
Please anyone, correct me if I am wrong. It is the best way for me to learn.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: round off number

 
0
  #5
Oct 13th, 2006
Originally Posted by vbcielle View Post
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
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: round off number

 
0
  #6
Oct 13th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: round off number

 
0
  #7
Oct 13th, 2006
The question is about rounding off the returned value and not rounding off while displaying. Read the first post.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: round off number

 
0
  #8
Oct 13th, 2006
Originally Posted by ~s.o.s~ View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: round off number

 
0
  #9
Oct 13th, 2006
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: round off number

 
0
  #10
Oct 15th, 2006
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
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