i found these routines somewhere on the web.  they worked for me:

#ifdef UNIX
#define StrChr(x,y) strchr(x,y)
#define StrCopy(x,y) strcpy(x,y)
#define StrLen(x) strlen(x)

//#include <stdlib.h>
//#include <string.h>
#endif
#include <pilot.h>
//#include <pilot.h>

/* fp.c - simple float/ascii conversion routines
 *
 * These routines handle signed fixed-point strings only; no exponential
 * notation is supported.
 *
 * Contains:
 *
 * float cvt_atof(char *srcP, short *fracP)
 *    converts the passed string to a float. If fracP is not 0, the
 *    number of fractional digits found will be returned there.
 *    An acceptable string is of the form [+-]digits[.digits]; leading
 *    whitespace is ignored.
 *
 * void format_number(float src, short digits, char *destP)
 *    converts the floating point src to a string; the value of digits
 *    determines the number of fractional digits to produce.
 *    The result will be rounded by adding 1/2 of the least-significant
 *    digit position's scaling value to the fractional part and then truncating
 *    the fractional part; e.g. 3.6 is rounded by adding .05 to the value.
 *
 * WARNING: if you use these functions with Pilot gcc, you MUST include
 * the function prototypes (below) in your code! Otherwise, gcc will
 * generate incorrect calls and your code won't work.
 *
*/


/* Internal prototypes */

static unsigned long cvt_atoul(char *);
static void cvt_ultoa(unsigned long,char *);

static void
cvt_ultoa(value,strP)    /* convert ulong to string */

unsigned long value;     /* value to convert */
char *strP;     /* destination buffer */
{
int i;
char *cP;
char tmpstr[16];  /* a 32 bit long can give up to 10 digits */

    if( value <= 9 )   /* a quick special case */
    {
 *strP++ = value + '0';
 *strP = '\0';
 return;
    }

    /* We convert the number backwards */
    for( cP = tmpstr+9, i = 10; i-- > 0; )
    {
 *cP-- = (value % 10) + '0';
 value /= 10;
    }

    tmpstr[10] = '\0';

    for( cP = tmpstr; *cP && (*cP == '0'); )
 ++cP;

    StrCopy(strP,cP);
}

static unsigned long
cvt_atoul(strP)     /* convert string to ulong */

char *strP;      /* string to convert */
{
unsigned long value;

    for( value = 0; (*strP >= '0') && (*strP <= '9'); )
    {
 value *= 10;
 value += *strP++ - '0';
    }

    return(value);
}

float cvt_atof(char *strP, char *fracP)     /* convert str to fp */

//char *strP;      /* string to convert */
//short *fracP;    /* if not null, return frac digits */
{
int i;
int neg;
char *cP;
unsigned long ipart,fpart;
float rslt;

    while( (*strP == ' ') || (*strP == '\t') )  /* trim whitespace */
 ++strP;

    if( *strP == '-' )     /* check for neg. */
    {
 neg = 1;
 ++strP;
    }
    else
    {
 neg = 0;

 if( *strP == '+' )    /* drop any sign */
     ++strP;
    }

    ipart = cvt_atoul(strP);   /* get integer part */
    if( fracP )
 *fracP = 0;

    if( (cP = StrChr(strP, '.')) )
    {
 i = StrLen(++cP);
 fpart = cvt_atoul(cP);   /* and frac part */
 rslt = (float)fpart;

 if( fracP )
     *fracP = i;
 while( i-- > 0 )   /* scale as needed */
     rslt /= 10.0;

 rslt += (float)ipart;
    }
    else
 rslt = (float)ipart;

    return( neg?-rslt:rslt );
}

void format_number(float value,short frac,char *strP)   /* format a number into
str */

    /* value to format */
     /* number of frac digits */
     /* destination */
{
int i;
unsigned long ipart, fpart;
float limit;
char *cP;
char str[16];

    if( frac > (sizeof(str) - 1) )  /* limit frac digits */
 frac = sizeof(str) - 1;

    if( value < 0.0 )
    {
 *strP++ = '-';
 value = -value;
    }

    ipart = (long)value;
    value -= (float)ipart;   /* recover frac part as int */
    for( limit = 1.0, i = frac; i-- > 0; )
    {
 value *= 10.0;
 limit *= 10.0;
    }

    value += 0.5;    /* do some rounding */
    if( value >= limit )
    {
 fpart = 0;    /* overflowed */
 ipart++;
    }
    else
 fpart = (unsigned long)(value);

    cvt_ultoa(ipart,strP);

    if( frac )
    {
 cvt_ultoa(fpart,str);
 strP += StrLen(strP);
 *strP++ = '.';

 for( i = StrLen(str); i++ < frac; )
     *strP++ = '0';   /* need some padding */
 StrCopy(strP,str);   /* and now the value */
    }
}


Reply via email to