How to convert floating value to integer with exact precision like 123.3443 to 1233443?

60,088
int i = (int) (f * 10000 + 0.5);
Share:
60,088
sur
Author by

sur

Updated on July 12, 2022

Comments

  • sur
    sur almost 2 years

    Sample code:

    int main() {
      float f = 123.542;
      int i = (int)f;
      printf("%d\n",i);
    }
    
  • Tomas Pruzina
    Tomas Pruzina about 12 years
    Not really generally helpful answer (though asker would get exactly what he asked for). It's (very) probably not what he had in mind.
  • PeterAllenWebb
    PeterAllenWebb over 8 years
    This will work, but assumes you know the number of decimal places you want in advance.