This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

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
  • Try this:

    unsigned int CalculatePercentage(int TotalComponent,int PartialComponent){
    return 100.0f*PartialComponent/TotalComponent;
    }
    

    You might want to read a C book, specifically a section on types and conversions between them. Otherwise prepare for more surprises like this one. Regards, - mike

Reply
  • Try this:

    unsigned int CalculatePercentage(int TotalComponent,int PartialComponent){
    return 100.0f*PartialComponent/TotalComponent;
    }
    

    You might want to read a C book, specifically a section on types and conversions between them. Otherwise prepare for more surprises like this one. Regards, - mike

Children