Converting a value from floating-point to a integer

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
User5327
Level 4
Level 4
Hi all,
How can I set Rounding mode for data type conversion (float to integer) ?
Is it possible?


float f_var = 1.6F;
int32_t i_var = 0;

i_var = (int32_t)f_var;

sometimes I need i_var == 1 another time I need i_var == 2




(I use XMC4500)


Is it same as in http://www.infineonforums.com/threads/2096-float-data-type
??

BR
Koumak
0 Likes
1 Reply
Not applicable
Hi Koumak,


One method for your example is to add 0.4 before casting if you want to round up...


i_var = (int32_t)(f_var + 0.4);



But if you want anything above 0.5 to round up then add 0.5 first


Best regards
Aaron
0 Likes