I found this in the archives some time back, but I couldn't find the
original message to reference.  Apologies to the orginal author.  Here's
the code:

#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

/* 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.
 *
*/

/* Prototypes to use for calling fp routines */

float cvt_atof(char *,short *);
void format_number(float,short,char *);

/* 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(strP,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(value,frac,strP)                /* format a number into str */

float value;                       /* value to format */
short frac;                        /* number of frac digits */
char *strP;                        /* 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 */
    }
}




                                                                                       
                        
                    Jaba Adams                                                         
                        
                    <[EMAIL PROTECTED]>               To:     "Palm Developer 
Forum"                    
                    Sent by:                                 
<[EMAIL PROTECTED]>                  
                    bounce-palm-dev-forum-23461@news.        cc:                       
                        
                    palmos.com                               Subject:     Where to 
find FloatToString?         
                                                             (FplFToA no good)         
                        
                                                                                       
                        
                    06/06/01 01:07 AM                                                  
                        
                    Please respond to "Palm Developer                                  
                        
                    Forum"                                                             
                        
                                                                                       
                        
                                                                                       
                        




Forgive me for asking yet another floating-point question, but I've
searched the archives and not found an answer.

I'm developing an app that includes floating-point input/display and
calculations, and needs to run on OS 1.0 and higher. I've been using the
FplAToF function to convert from ui fields to floating point nums, but I'm
finding the reverse to be painful. Using FplFToA, I get a string that's in
scientific notation, but I'd like to be able to truncate the string length
and specify the number of decimal places to display.

There was some mention of a FloatToString function in the archives, but I
haven't been able to find the code. Does anyone know where the sample code
lives? Will this allow me to specify decimal places?

Thanks!

Jaba Adams


--
For information on using the Palm Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/tech/support/forums/





-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to