Hi,
My previous implementation had some bugs in it... please use this implementation:
Here is how you can call the functions:
unsigned char senderBufferPtr[100];
ftoaMsg(senderBufferPtr, "thenumberis=" , 125.123123 , 2);
the code:
void ftoaMsg(char *Buffer, char *Msg, float Number, unsigned char decimalPrecision)
{
long int numf;
unsigned char strSize;
char * ptr;
(void) strcpy(Buffer, Msg);
strSize= strlen(Buffer);
ptr = &Buffer[strSize];
ftoa( Number, decimalPrecision , ptr);
return;
}
const float ROUND[6]={0.49,0.05,0.005,0.0005,0.00005,0.000005};
void ftoa(float fnum, unsigned char decimals, unsigned char *str)
{
float scale;
unsigned char u1,u2;
if (fnum<0.0)
{
fnum=-fnum;
*str++='-';
}
if (decimals>5)
decimals=5;
fnum += ROUND[decimals];
u1=0;
scale=1.0;
while (fnum>=scale)
{
scale *= 10.0;
++u1;
}
if (u1==0)
*str++='0';
else
while (u1--)
{
scale=floor(0.5+scale/10.0);
u2=(unsigned char)(fnum/scale);
*str++=u2+'0';
fnum -= scale*u2;
}
if (decimals==0)
{
*str=0;
return;
}
*str++='.';
while (decimals--)
{
fnum *= 10.0;
u2=(unsigned char) fnum;
*str++ = u2+'0';
fnum -= u2;
}
*str=0;
}