Why does dividing a float by an integer return 0.0?

It’s because you’re doing integer division.

Divide by a double or a float, and it will work:

double scale = ( n / 1024.0 ) * 255 ;

Or, if you want it as a float,

float scale = ( n / 1024.0f ) * 255 ;

Leave a Comment