Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone - converting float to integer

This is a simple question:

Is this a correct way to get an integer part from a float division?

int result = myFloat / anInteger;

this is working, but I am not sure if it is the best way.

thanks for any help.

like image 238
Duck Avatar asked Mar 23 '26 03:03

Duck


1 Answers

To truncate:

int result = (int)(myFloat / anInteger);

Other conversion options:

#include <math.h>
int result = (int)ceilf(myFloat / anInteger);
int result = (int)roundf(myFloat / anInteger);
int result = (int)floorf(myFloat / anInteger);
like image 195
kennytm Avatar answered Mar 24 '26 19:03

kennytm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!