Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

can't get a good float to string and vise versa function .....

6 views
Skip to first unread message

Dan Miller

unread,
Mar 12, 2001, 2:41:28 AM3/12/01
to
i've been looking all over the message boards and internet for some sort of
function that returns a string value of a float and a function that returns
the float value of a string. every one that i tried has some sort of
problem with it .... example:

i used this function to convert a string to a float:

double StrToDbl( CharPtr pString )
{
FlpCompDouble dObj;

dObj.fd = FlpAToF( pString );
return ( dObj.d );
}

i use a double var to hold information for the return value in the calling
function, which i then use to sum the values of multiple pStrings in a loop:

double dAmount;
for ( loop code )
{
dAmount += StrToDbl( pString );
}

the error produced by this code is such:

Link Error : cbook.c: '_d_add' referenced from 'drawTable' is undefined.
(which i know is a result of the adding line above ... if i comment it out
or hard-code a double value instead of the function it builds fine )

i also use this function to get a string from a float value:

CharPtr DblToStr( double pFloat )
{
FlpCompDouble flpObj;
CharPtr sFloat;

sFloat = "0";
// Convert a float to a string
flpObj.d = pFloat;
FlpFToA( flpObj.fd, sFloat );

return ( sFloat );
}

this compiles fine, but i get a bus error on the emulator when i run it
there.


does anyone know why it is so difficult to convert a float to string and
vice versa? please give me help with this ... this is an important piece of
the puzzle that i need to figure out, and i've exhausted all my resources
that i can think of right now.

thank you in advance,
-Dan-


Jay Hong

unread,
Mar 12, 2001, 10:08:49 AM3/12/01
to
if "Double add" is undefined, assuming you are using CW, go to project
settings,code generation,68K processor,floating point, and be sure it is set
to "PalmOS"

Dan Miller

unread,
Mar 12, 2001, 2:16:48 PM3/12/01
to
either i don't have the include files needed to run the printDouble()
function, or i don't have it set up right in CodeWarrior to include them.
the files that i get compile errors on are:

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

the compiler says they don't exist. perhaps this is because i'm using
codewarrior lite. perhaps its a setting in the project settings that i
failed to set up. i'm still learning how codewarrior works, so any help
would be much appreciated. thank you

-Dan-

--

"Ben Combee" <com...@techwood.org> wrote in message
news:fb688Ky...@www.massena.com...
> "Jay Hong" <jh...@lancercorp.com> wrote in message
> news:djY#tGyqA...@www.massena.com...
> > What is a FlpCompDouble? (I searched the CW directory and could not find
> it)
> > Also, do you realize that FlpFtoA will give you a string in exponential
> > format?
>
> The whole float-to-string issue is a VERY frequently asked question.
>
> Why not check the FAQ?
>
> Its at http://www.cyberport.com/~tangent/palm/faq/. It points to an
article
> on the subject at
> http://www.cyberport.com/~tangent/palm/faq/articles/float.html. That
> article has code for a function called printDouble() which does what you
> want.
>
> --
> Ben Combee
> Veriprise Wireless <http://www.veriprise.com>
>
>


Jay Hong

unread,
Mar 12, 2001, 1:24:29 PM3/12/01
to

Ben Combee

unread,
Mar 12, 2001, 1:35:51 PM3/12/01
to
"Jay Hong" <jh...@lancercorp.com> wrote in message
news:djY#tGyqA...@www.massena.com...

The whole float-to-string issue is a VERY frequently asked question.

Dan Miller

unread,
Mar 12, 2001, 12:18:23 PM3/12/01
to
thank you so much ... that was the problem.

but on the other hand, i can't convert the ending float result to a string.
the function below compiles fine, but causes a bus error on the emulator
when running:

CharPtr DblToStr( double pFloat )
{
FlpCompDouble flpObj;
CharPtr sFloat;

sFloat = "0";
// Convert a float to a string
flpObj.d = pFloat;
FlpFToA( flpObj.fd, sFloat );

return ( sFloat );
}

i think i saw a post on this same question before, but i don't think it was
answered. any ideas?

thanks,

-dan-

--


"Jay Hong" <jh...@lancercorp.com> wrote in message

news:Ib1VYZw...@www.massena.com...

Mark Hamann

unread,
Mar 12, 2001, 6:26:33 PM3/12/01
to
There seems to be a float/ascii related question about once a month. If you
want floats and not doubles, go back to October 15-17, 2000 in this news
group for the exchange there (Question on Float to Ascii, Ascii to float,
add and subtract). The responder (Mike Margerum) included the source for
two functions that are probably what you are after. Atleast they are
exactly what I'm after.

I have to warn you that I am just getting into this chaotic float/ascii
mess, so I haven't tried this out yet. I still have brain-fry from not
realizing that Flp* and Fpl* are different (those sneaky Palm library
programmers:/). Just wanted to tell you far back you need to go to find
something truly useful in this area.

Mark Hamann

Dan Miller <dmi...@dmxcoding.com> wrote in message
news:A2Dtyfs...@www.massena.com...

Ben Combee

unread,
Mar 12, 2001, 2:22:09 PM3/12/01
to
"Dan Miller" <mill...@hotmail.com> wrote in message
news:xRVP2hy...@www.massena.com...

> either i don't have the include files needed to run the printDouble()
> function, or i don't have it set up right in CodeWarrior to include them.
> the files that i get compile errors on are:
>
> #include <PalmTypes.h>
> #include <PalmCompatibility.h>
>
> the compiler says they don't exist. perhaps this is because i'm using
> codewarrior lite. perhaps its a setting in the project settings that i
> failed to set up. i'm still learning how codewarrior works, so any help
> would be much appreciated. thank you

The code in printDouble was written for the 3.1 SDK, then ported to the 3.5
SDK. You would be best served by updating the the PalmOS 3.5 SDK,
especially if you're doing new development. Barring that, just remove those
two lines -- they aren't needed for SDK 3.1.

Dan Miller

unread,
Mar 13, 2001, 12:35:52 PM3/13/01
to
i can't access the newsgroup that far back .....

if possible, could you just send me the post ... or perhaps the code that
converts double-to-string? i'd really appreciate it.

thanks,

--

-Dan-


"Mark Hamann" <mha...@hampton-power.com> wrote in message
news:73oAjv0...@www.massena.com...

Dan Miller

unread,
Mar 13, 2001, 8:09:47 PM3/13/01
to
i'd just thought i would let you all know that i re-wrote someone's
Double-To-String function so it should work for everyone (i don't remember
where i got the source originally .. i've been looking all over the place
and pulled code from here and there ... so if this function looks like
something you did, please let me know so i can give credit where its due ).
i've noticed that there are lots of posts on this forum of people looking
for good 'double-to-string' and 'string-to-double' functions, and the
functions i found either have compile errors, or create 'bus' errors in the
emulator. the one i fixed up seems to do the trick .... but i'm not
guarrenteeing that it is completely bug-free. just the testing that i have
done seems to work.

here's the function that i did ...... the enviroment is as follows:
Compiler: CodeWarrior ( i started out with lite, but when i upgraded to
SDK 3.5 i think it erased the 'lite' part )
SDK version: 3.5 with upgrade
Emulator: 3.0a8

/***************************************************************************
************
*
* FUNCTION: DblToStr
*
* DESCRIPTION: Sets the string to a double value
*
* PARAMETERS: CharPtr str, double flpNumber, Int numFractDigits, Boolean
showSign
* ( str is the pointer receiving the converted double, flpNumber is the
* passed in double, numFractDigits is how many fractional places are
* to be returned, showSign tells the function if you want the string
* to include the +/- sign to be displayed. )
*
* RETURNED: nothing
*

****************************************************************************
***********/

static void DblToStr( CharPtr str, double flpNumber, Int numFractDigits,
Boolean showSign)
{
Long longNumber;
double flpIP, zeros, round;
Int i, strLen, startPtr, fracPos;
CharPtr tempStr;
Boolean isNeg;

tempStr = str;
isNeg = false;
startPtr = 0;
fracPos = 0;

// Put your custom Alert form here
if (numFractDigits < 0 || numFractDigits > 9)
FrmAlert(FloatErrorAlert);

if ( flpNumber < 0 )
{
isNeg = true;
flpNumber *= -1;
}

zeros = 1.0;
for (i=0; i<numFractDigits; i++)
zeros *= 10;

round = 0.5/zeros;

flpNumber += round;
flpIP = (int) flpNumber;
flpNumber = flpNumber - flpIP;


if ( ( isNeg) && (showSign) )
{
tempStr[startPtr] = '-';
startPtr += 1;
}
else if ( (!isNeg) && (showSign) )
{
tempStr[startPtr] = '+';
startPtr += 1;
}

// Get integer part and copy to temp string
StrIToA( &tempStr[startPtr], (long) flpIP );

// Get the length of the string and append a '.' to the end if it
strLen = StrLen( tempStr );
tempStr[strLen] = '.';

// Pad the decimal places with zeros
strLen = StrLen( tempStr );
for (i=0; i < numFractDigits; i++)
tempStr[strLen+i] = '0';

// Get the fractional part and append it after the '.'
longNumber = flpNumber * zeros;

if ( longNumber != 0 ) // only print if it is not a zero value - the
padding takes care of this
{
for ( i = longNumber*10; i<zeros; i *= 10 )
fracPos += 1;

for (i=1; i <= fracPos; i++ )
tempStr[strLen+i] = '0';

StrIToA( &tempStr[strLen+fracPos], longNumber );
}

// Append a null to the end of the string
strLen = StrLen( tempStr );
tempStr[strLen] = '\0';

// Point the new string to the given pointer
StrCopy( str, tempStr );
}


i tried to comment here and there, but probably not enough. anyhow, i hope
this helps anyone who is looking for the double-to-string function. below i
will post the function that i used to convert string-to-double (i didn't
write this one, but it seems to work good):

/***********************************************************************
*
* FUNCTION: StrToDbl
*
* DESCRIPTION: Gets a string value and returns a double
*
* PARAMETERS: CharPtr
*
* RETURNED: double
*
***********************************************************************/


double StrToDbl( CharPtr pString )
{
FlpCompDouble dObj;

dObj.fd = FlpAToF( pString );
return ( dObj.d );
}


this one seems much more simple that the double-to-string function.


0 new messages