c - type casting float to int -


i getting float input user code.

1. float change = getfloat(); 2. change *= 100; 3. int remainder = change; 

however, when user inputs 4.2 line 2 turns change variable 419.999969 , line ends 419. how change program effectively change float int.

one way round nearest whatever. simple version be

change = (int)(change + 0.5); 

(this go between steps 2 , 3.) rounds less 0.5 down, , greater or equal 0.5 up.

there may additional complications if want round negative numbers, or if want round other nearest 1, should started.


Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

android - Pass an Serializable object in AIDL -