Float to Integer conversion for MSP430

Hello forum members,

Does anyone having a code for converting float value to integer for MSP430 controller?? Even assembly routine will also be helpful.

Thanks in advance.

If you aren’t concerned about rounding you could just cast it to an int.

Leon

And if you want to keep some decimal digit you can use the “fixed point” trick:

This is an example to keep two decimal digits;

float f=1.2345678;

int i;

i=(int)(f*100);

i will be 123, with 1 being the integer part and 23 being the decimal part. All other digits are truncated.

If then you want to print i, use

printf(“%d.%d\n”,i/100,abs(i%100));

(assuming the MSP430 library has printf, and assuming it is redirected to something useful)