how to round off the return value

hello anyone here wants to help me...i want the return value to be rounded off for example:
TotalComponent=235
PartialComponent=90
per=38.3%

but with this function i always get 0 % anyone here want to sugest what should be done

unsigned int CalculatePercentage(int TotalComponent,int PartialComponent){
int per;

per= (PartialComponent/TotalComponent)*100;
return (float)per;
}

Parents
  • "per=38.3

    No, per is a signed integer as are the function parameters, one of which divides into the other to yield a signed int, which is further divided by an int and the integer result of all that is assigned to the int named 'per'. Casting an int to a float yields a non-fractional float. Returning that temporary float value as an unsigned int merely truncates the fractional part of the float that in this case never existed.

    Think about how to make some subexpressions floating point or scaled fixed point integers and how to adjust your return type or your interpretation of the return type to suit your needs.

Reply
  • "per=38.3

    No, per is a signed integer as are the function parameters, one of which divides into the other to yield a signed int, which is further divided by an int and the integer result of all that is assigned to the int named 'per'. Casting an int to a float yields a non-fractional float. Returning that temporary float value as an unsigned int merely truncates the fractional part of the float that in this case never existed.

    Think about how to make some subexpressions floating point or scaled fixed point integers and how to adjust your return type or your interpretation of the return type to suit your needs.

Children
More questions in this forum