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; }
hello mike tried your code how will i know if it return 38.3% i transfered the code in c
per=CalculatePercentage(235,90); printf("Percent %d",per);
the return value is 38% the .3 is disregarded
even if i change per to float
main(){ float per; per=CalculatePercentage(235,90); printf("Percent %f",per); }
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 );
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 ); }
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!