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;
}

  • "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.

  • Pardon me. I said "... one of which divides into the other to yield a signed int, which is further divided by an int ..."

    That should be multiplied by an int. Multiplying the integer result of division does not suddenly cause a fractional result of the division to appear. Like I said, think about the 'type of' and 'results of' your subexpressions.

  • 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);
    }
    


    the return value is 38% the .3 is disregarded

  • 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!