The rounding is done in your display function. Here is C example:
// percent calculation, wants 100 * 90/235 --> 38.3 %
#include <stdio.h>
double CalculatePercentage(double TotalComponent, double PartialComponent)
{
return 100*PartialComponent/TotalComponent;
}
int main()
{
printf("100 * 90/235 --> %0.1f%c", CalculatePercentage(235, 90), '%');
getchar(); // wait for key press
return 0;
}
And here is C++ example:
// percent calculation, wants 100 * 90/235 --> 38.3 %
#include <iostream>
#include <iomanip>
using namespace std;
double CalculatePercentage(double TotalComponent, double PartialComponent)
{
return 100*PartialComponent/TotalComponent;
}
int main()
{
cout.setf(ios::fixed);
cout<< setprecision(1) << "100 * 90/235 --> " << CalculatePercentage(235, 90) << '%' << endl;
cin.get(); // wait for key press
return 0;
}
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
The question is about rounding off the returned value and not rounding off while displaying. Read the first post.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
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.
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
Try out something like this, though its a bit round about way, but should work:
double return_round( double value, double total )
{
char buffer[BUFSIZ] = {'\0'} ;
double percentage = ( value / total ) * 100 ;
printf( "\nActual number: %lf", percentage ) ;
sprintf(buffer, "%0.2f", percentage) ;
if ( buffer[ strlen( buffer ) - 1 ] >= '5' )
buffer[ strlen( buffer ) - 2 ] += 1 ;
buffer[ strlen( buffer ) - 1 ] = '\0' ;
printf( "\nThe rounded string is: %s", buffer ) ;
return atof( buffer ) ;
}
Hope it helped, bye.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
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.
WaltP
Posting Sage w/ dash of thyme
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943
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.
It may be quite possible that the data rounded off is not used for calculations or used in calcultions which requires only the rounded off values. It could also be written to a file and used only for data analysis purposes, or maybe the OP had some other idea in his mind or maybe it was part of an assignment given at the college to test his coding skills, you never know.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
It may be quite possible that the data rounded off is not used for calculations or used in calcultions which requires only the rounded off values. It could also be written to a file and used only for data analysis purposes, or maybe the OP had some other idea in his mind or maybe it was part of an assignment given at the college to test his coding skills, you never know.
True -- but with all theif...when...maybe's that get posted here (and you pointed out in this post), noone would get any answers because few would want to post a response based on all the if...when...maybe's to cover all bases. I will respond to what is directly posted and may make an educated guess as to the usage. If I guess wrong, then the OP obviously didn't communicate his problem/needs well enough. His fault, not mine.
:twisted:
So listen up, noobs! Post enough information that we don't have to guess at what you need to know. You need to tell us! That way us folks that are trying to help you pass your classes won't get into arguments like this one!
:p ;)
WaltP
Posting Sage w/ dash of thyme
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943
So listen up, noobs! Post enough information that we don't have to guess at what you need to know. You need to tell us! That way us folks that are trying to help you pass your classes won't get into arguments like this one!
:p ;)
lol.. :D I agree with you on that one, old buddies fighting over beginners is indeed not a good sight :)
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
To resurrect a 4 year old post with completely wrong information is pretty bad.
double x=1.5
printf("%ld",floor(x) );
will output1, not 2.
WaltP
Posting Sage w/ dash of thyme
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943
Always keeping as much precision as possible is definately not a bad option, however there are others.
For instance there is no point in keeping 15 decimal places of precision if the value represents a real world item with a fixed quantum of precision, for example cash. I can have £6.349999 because cash is not available in that quantum, I must have either £6.34 or £6.35.
The problem is I see cash as having decimal places and force that perception onto the computer where it does not need it. Instead of holding the value in pounds I can hold it in pence, 635p as an integer. Now the physical limit on the quantum change of the value is matched by the computers limit on the quantum change of the variable, 1 in both cases.
Back to the original problem. You are holding a percentage as an integer and want it rounded off to 1 decimal place. Everyone is saying use a float and perform werid calculations to try to round it that are bound to not work quite correctly at some point due to the non-exact nature of floats.
Why not hold it in an integer in units of 1/10 of 1%?
I am not saying this is the right way. That is dependent on your program. What I am saying is if you really want to limit the number of decimal places you are using this is the way to do it. However if in fact limiting the number of decimal places you are using is not that important then as WaltP says above, maintain maximum precision until the very last second you need to, i.e. on display.
One final advantage of using scaled integers instead of floats. if you happen to be programming for a low powered micro-controller it may not even have a floating point library but if it does it will certainly be very costly in processor cycles to use compared to simple integer arithmetic.
EDIT: Sorry I hadn't noticed this was an ancient thread when I post this reply.
Banfa
Practically a Master Poster
600 posts since Mar 2010
Reputation Points: 486
Solved Threads: 92
EDIT: Sorry I hadn't noticed this was an ancient thread when I post this reply.
When answering threads you need to read them first. Especially the last post made. There is no point answering if what you're going to say has already been said. And by reading the thread you might have read my post.
WaltP
Posting Sage w/ dash of thyme
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943