# problem float to int

**URL:** <https://forums.digi.com/t/problem-float-to-int/10010>\
**Category:** Rabbit\
**Created:** [December 18, 2007, 8:48am UTC](https://forums.digi.com/t/problem-float-to-int/10010 "2007-12-18T08:48:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sgianty](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@sgianty](https://forums.digi.com/u/sgianty)\
**Post date:** [December 18, 2007, 8:48am UTC](https://forums.digi.com/t/problem-float-to-int/10010/1 "2007-12-18T08:48:21Z")

</div>

I have a problem with the conversion from float to int.

float FAux;  
int VMin;

strcpy(ss,“27.9”);  
FAux=atof(ss); [//FAux=27.9](https://FAux=27.9)  
if(\_xtoxErr) break;  
FAux=FAux\*10; //FAux=279  
VMin=(int)FAux; //VMin=278 Why?

Why is there 278 into VMin?

I have Dinamic C 9.52.

Thanks

---

<div class="post-metadata">

**Author:** ![dwstojan](https://avatars.discourse-cdn.com/v4/letter/d/49beb7/32.png) [@dwstojan](https://forums.digi.com/u/dwstojan)\
**Post date:** [December 19, 2007, 2:30pm UTC](https://forums.digi.com/t/problem-float-to-int/10010/2 "2007-12-19T14:30:42Z")

</div>

[QUOTE=sgianty;1168]I have a problem with the conversion from float to int.

float FAux;  
int VMin;

strcpy(ss,“27.9”);  
FAux=atof(ss); [//FAux=27.9](https://FAux=27.9)  
if(\_xtoxErr) break;  
FAux=FAux\*10; //FAux=279  
VMin=(int)FAux; //VMin=278 Why?

Why is there 278 into VMin?

I have Dinamic C 9.52.

[/quote]

Floating point math has limited precision, especially single precision floating point. There are many numbers that cannot be truly represented in floating point format (however they are generally accurate enough - but NOT in your particular context). Simple fix for positive numbers:

VMin = (int) (FAux + 0.5);

If you want to handle negative numbers too as well you’ll want to test the  
sign and add or subtract 0.5 accordingly. Good luck!

P.S.- To answer your question WHY it came out 278 it’s because 27.9 was actually represented by 27.899999619 and when multiplied by 10.0 came out to 278.99999619 and the float to int conversion stopped when it hit the decimal point.
