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 Reply Children
  • Isn't it obvious?
    You really really need to read a book on C. Getting answers to questions like these in forums only postpones the inevitable - reading the books.

    - mike

  •  per = CalculatePercentage( 235, 90 );
     printf( "Percent %d", per );
    

    the return value is 38% the .3 is disregarded

    As Mike says, the reson for this should be obvious - it's textbook stuff.

    main() {
     float per;
     per = CalculatePercentage( 235, 90 );
     printf( "Percent %f", per );
    }
    

    the return value is 38% the .3 is disregarded

    How have you checked the return value?

    Obviously, if the return value has been truncated before calling printf, then just changing the format specifier in the printf cannot magically re-generate the discarded information!

    Or maybe you just changed your source code, and forgot to rebuild it...?

    PS Note how the addition of a little whitespace makes your code so much easier to read!