In email, Andre S. Chen wrote:
>I am making use of the FloatToString function you posted on Palm Dev Forum
on July 14.
>I think there may be a bug, however. It seems to turn a float between 0
and -1 into a positive
>number string. I'm not much of a programmer so I wanted to see if you were
getting the same
>results.
I looked into it and here's a fix. (Yeah, I've got a feeling there's a more
elegant way to do all of
this. But I don't really have the time right now for more than a simple
patch. Seems to work.)
void FloatToString (double value, char * buffer, int round)
{
long iValue;
double dDecimal;
long iDecValue;
int i;
char sResult[50];
char sDecimal[50];
char *sTemp = sResult;
iValue = value;
if( value < 0 )
{
dDecimal = -(value - iValue);
// Make sure that negative #s where -1 < n < 0
// get handled correctly.
if( 0 == iValue )
StrCopy( sTemp++, "-" );
}
else
dDecimal = value - iValue;
if (dDecimal < 0.000000001) dDecimal = 0;
// Convert integer portion to string
StrIToA( sTemp, iValue);
if (StrLen(sTemp) < 1) StrCopy(sTemp, "0");
StrCat(sResult, ".");
// Round decimal portion
for (i = 1; i <= round; i++)
dDecimal = dDecimal * 10;
iDecValue = dDecimal;
if (dDecimal - iDecValue >= 0.5) iDecValue++;
// Add decimal portion
StrIToA (sDecimal, iDecValue);
// Add leading zeros if neccessary
if (StrLen (sDecimal) < round)
for (i = 1; i <= round - StrLen(sDecimal); i++)
StrCat (sResult, "0");
StrCat (sResult, sDecimal);
// Copy into return string
StrCopy (buffer, sResult);
}
--
For information on using the ACCESS Developer Forums, or to unsubscribe, please
see http://www.access-company.com/developers/forums/