We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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; }
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
hello mike im sori thats suppose to be
return per;
okie i'll try your code thanks for the reply
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!