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

unsigned int CalculatePercentage(int TotalComponent, int PartialComponent){
return int((PartialComponent/TotalComponent)100);
}

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

unsigned int CalculatePercentage(int TotalComponent, int PartialComponent){
return int((PartialComponent/TotalComponent)100);
}

You must multiply with 100 (for multiplaying use * ).

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.

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

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;
}

The question is about rounding off the returned value and not rounding off while displaying. Read the first 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.

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.

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.

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.

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 the if...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 ;)

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 :)

lol.. :D I agree with you on that one, old buddies fighting over beginners is indeed not a good sight :)

im sorry guys if i make all of you confuse.... i want it to be round of in the function
because i'll use the returned value for another function

unsigned int CalculatePercentage(int TC,int PC){
return 100.0f*PC/TC;
}

will the code be enough? even if i use unsigned int will i still get a decimal value and round it off?

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

unsigned int CalculatePercentage(int TotalComponent, int PartialComponent){
return int((PartialComponent/TotalComponent)100);
}

Use floor fun. call which was defined in math.h

ex:
double x=1.5
printf("%ld",floor(x) );

o/p will be 2
instead of 1.5

To resurrect a 4 year old post with completely wrong information is pretty bad.

double x=1.5
printf("%ld",floor(x) );

will output 1, not 2.

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.

Edited 6 Years Ago by Banfa: n/a

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.

This article has been dead for over six months. Start a new discussion instead.