-----Original Message-----
From: Wadhwa, Harmit
To: Laor, Boaz
Sent: 2/21/01 1:26 PM
Subject: string to float or string to double

Hey everyone,

I am trying to use these functions that I have found in the archives
that
everyone
says work fine to convert a string to a float or double.  I get errors
of
the following
sort:
Link Error   : TransferingFundsForm.c: '_f_itof' referenced from
'MyStrToFloat' is undefined.

Link Error   : TransferingFundsForm.c: '_f_div' referenced from
'MyStrToFloat' is undefined.

Link Error   : TransferingFundsForm.c: '_f_add' referenced from
'MyStrToFloat' is undefined.

If anyone has used these specific functions I would greatly appreciate
some
guidance.

Thanks in advance,
Harmit



Double Function
************************************************************************
*

#include <PalmOS.h>
#include <PalmTypes.h>
#include <PalmCompatibility.h>

#include "mbox.h"

#if defined(USELIBGLUE)
#include <Libraries/PalmOSGlue/TxtGlue.h>
#define PalmIsDigit TxtGlueCharIsDigit
#else
#define PalmIsDigit TxtCharIsDigit
#endif


// Routine to convert a string to a double -- 
// Allowed input is in fixed notation ddd.fff
// This does not use MathLib.
//

double strToDouble(const char* str)
{
    Int i, start, length, punctPos;
    double result, sign, fractPart;

    result = fractPart = 0.0;
    length = punctPos = StrLen(str);

    start = 0;
    sign = 1.0;
    if (str[0] == '-') {
        sign = -1.0;
        start = 1;
    }

    for (i = start; i < length; i++)    // parse the string from left to
right converting the integer part
    {
        if (str[i] != '.') {
            if (PalmIsDigit(str[i]))
                result = result * 10.0 + (str[i] - 0x30);
            else {
                MessageBox("Non-digit character encountered in FP
value.",
                        kMBError);
                return 0.0;
            }
        }
        else {
            punctPos = i;
            break;
        }
    }

    if (str[punctPos] == '.')   // parse the string from the end to the
'.'
converting the fractional part
    {
        for (i = length - 1; i > punctPos; i--) {
            if (PalmIsDigit(str[i]))
                fractPart = fractPart / 10.0 + (str[i] - 0x30);
            else {
                MessageBox("Non-digit character encountered in FP
value.",
                        kMBError);
                return 0.0;
            }
        }
        result += fractPart / 10.0;
    }

    return result * sign;       // correcting the sign
}
************************************************************************
****
*********************
Float function

float StrToFloat(char *Source)
{
        char bp[12];
        char ap[12];
        char div[12];
        float apt,bpt;
        char ScrArr[12];
        int i,len,j;
        Boolean Point=false;
        
        if (StrChr(Source,'.')==NULL)
                return (float)StrAToI(Source);
        
        StrCopy(ScrArr,Source);
        len=StrLen(Source);
        j=0;
        div[j]='1';
        for(i=0;i<len;i++)
        {
                if(ScrArr[i]=='.')
                        Point=true;
                if(!Point)
                        bp[i]=ScrArr[i];
                else
                {
                        if (j==0)
                        {
                                bp[i]='\0';     
                                i++;
                        }       
                        ap[j]=ScrArr[i];
                        j++;
                        div[j]='0';
                }               
        }
        ap[j]='\0';
        div[j+1]='\0';
        bpt=(float)StrAToI(bp);
        apt=(float)StrAToI(ap);
        
        apt=apt/StrAToI(div);
        return bpt+apt;
}

-- 
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