I had problems with FlpFToA in the past, as well and I use this function for
that now (originally from this forum, but I did find a bug in it if you
tried to round it to 0 decimal places.. noted in the code):

// Note that this function does not do bounds checking on the passed buffer,
// so be sure it is big enough to handle the data!
void FloatToString (double value, char * buffer, int round)
{
 // this function came from a combination of Thiago Rossato and
 //  Noah Young via the Palm Developer Forum
 long iValue;
 float dDecimal;
 long iDecValue;
 int i;
 char sResult[50];
 char sDecimal[50];

 iValue = value;

 dDecimal = value - iValue;

 // if dDecimal >= 1, then iValue should actually be greater by dDecimal
 //   this happens in situations of numbers that can't be accurately
 //   represented as a double.. (7 is 6.9999999997, but dDecimal is 1 after
 //   the above) (this was the bug I mentioned)
 if (dDecimal >= 1)
 {
  i = dDecimal;
  iValue = iValue + i;
  dDecimal = dDecimal - i;
 }
 // Make sure rounding will still work if value is negative
 if (value < 0)
  dDecimal = -dDecimal;

 if (dDecimal < 0.000000001) dDecimal = 0;

 if (!round && dDecimal >= 0.5) iValue++;

 // Convert integer portion to string
 StrIToA(sResult, iValue);
 if (StrLen(sResult) < 1) StrCopy(sResult, "0");
 if (round)
 {
  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);
}

And.. since you asked.. This is how I use the existing funtion to go back
the other way:

double StrAToF(char *str)
{
 FlpCompDouble theCompFloat;
 double theFloat;

 // Convert a string to a float
 theCompFloat.fd = FlpAToF(str);
 theFloat = theCompFloat.d;
 return theFloat;
}

----------
Bradly J. Barton - [EMAIL PROTECTED]
Jenies Technologies Incorporated
(972) 602-1835
http://www.JTI.net
http://HandAble.com



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