Getting integer result when dividing float by integer in c

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I have been trying to get an integer value when dividing float by integer , but I could not come up with what I am looking for .

int a = 3 ;
float b = 15.50 ;
int result ;
result = int b/a

I have not programmed for long time , and I know this is basic and I should be aware of it , I really appreciate any help . Thank you

Answers

You define result to be an integer:

int result;

This forces the result of b/a to be an integer. In case this is what you intended: cast the result of b/a to integer and store it in result:

result = (int)floor(b/a);

Use floor (or ceil) for well-defined results in result and clear and concise code.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/20411498/getting-integer-result-when-dividing-float-by-integer-in-c

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils