Sergio Carvalho wrote:
>> Are there any functions that convert Float variables to/from strings?
>> StrPrintf doesn't seem to support the %f format specification...
"Roberto Amorim" <[EMAIL PROTECTED]> wrote in message
news:228@palm-dev-forum...
> Hi Sergio,
> I had this problem last week and Thiago Rossato sent me this function.
> I hope this help you too.
Thanks a bunch for posting that function. It was very helpful. However,
it has a bug that causes it to not process negative numbers as one might
expect. The code below fixes that problem by changing the decimal part
of the number to a positive number for processing. Seems to work great
now.
Thanks again,
Noah Young (Metta4)
void FloatToString (float value, char * buffer, int round)
{
long iValue;
float dDecimal;
long iDecValue;
int i;
char sResult[50];
char sDecimal[50];
iValue = value;
dDecimal = value - iValue;
// Make sure rounding will still work if value is negative
if( value < 0 )
dDecimal = -dDecimal;
if (dDecimal < 0.000000001) dDecimal = 0;
// Convert integer portion to string
StrIToA(sResult, iValue);
if (StrLen(sResult) < 1) StrCopy(sResult, "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 Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/