Check out http://www.davidbray.org/palm/dtostr.html and
http://www.davidbray.org/palm/strtod.html

The functions are strToDouble and doubleToStr - I got the name wrong from
memory.
The functions I use - quite successfully - are based on David's algorithms.
I include them here:

----------------------------------------------------------------------------
------
/**
 *  Convert double value to C string. The string can have up to 9
 *  fractional digits (digits after decimal). Sufficient space must
 *  be provided to store the resulting string - 20 bytes of storage is
 *  recommended.
 *
 *  @param str pointer to storage for the resulting string data.
 *  @param flpNumber the floating point (double) number to be converted to
 *    an ascii string.
 *  @param numFractDigits max number of digits to be included after the
 *    decimal point. If less than the actual number of fraction digits
 *    in the number, the value will be rounded up to specified number
 *    of fraction digits.
 *  @param fixedFractDigits true (the default value) if the specified number
 *    of fraction digits are to be generated, false if trailing zero digits
 *    are to be trimmed from the string representation of the number.
 *  @return the callers storage buffer pointer (str).
 *
 */
char* doubleToStr(char* str, double flpNumber, int numFractDigits,
                  Boolean fixedFractDigits) {
  long  longNumber;
  double flpIP, zeros, round;
  int i, remainder, strLen;
  char sign = ' ';
  if(numFractDigits < 0)
    numFractDigits = -numFractDigits;

  if(numFractDigits > 9)
    numFractDigits = 9;

  if (flpNumber < 0.0)
    { flpNumber = -flpNumber;
      sign = '-';
    }

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

  round = 0.51/zeros;

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

  // REC remove addition of annoying leading space on postive number
  if(sign == '-') {
    str[0] = sign;
    StrIToA(&str[1], (Int32) flpIP);
  }
  else {
    StrIToA(str, (Int32) flpIP);
  }
  strLen = StrLen(str);     // put in the decimal point and terminate the
string
  //Remove annoying decimal point when no fractions requested
  if(numFractDigits > 0) {
    str[strLen] = '.';
    str[numFractDigits+strLen+1] = '\0';
    strLen++;
  }

  longNumber = flpNumber * zeros;   // fractional part

  for (i=numFractDigits+(strLen-1); i>(strLen-1); i--)  // convert the
integer part
    { remainder = longNumber % 10;
      str[i] = remainder + 0x30;
      longNumber /= 10;
    }

  if(!fixedFractDigits && numFractDigits > 0) {
    // trim any trailing zero digits
    int pos = StrLen(str) - 1;
    for(; numFractDigits > 0;
        pos--, numFractDigits--) {
      if(str[pos] == '0')
        str[pos] = '\0';
      else
        break;
    }
    if(!numFractDigits)
      str[pos] = '\0'; // get rid of decimal
  }
  return str;
}

// Routine to convert a string to a double -- Allowed input is in fixed
notation ddd.fff
//
double strToDouble(char* str)
{ int  i, start, length, punctPos;
  double  result, sign, fractPart;
  //UInt16 attributes;

  if(str == 0 || *str == 0)
    return 0.0;

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

  // REC - skip any leading whitespace
  for(start = 0; str[start] != '\0' && TxtCharIsSpace(str[start]); start++)
    ; // intentional null statement

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

  for (i=start; i<length; i++) {  // parse the string from left to right
converting the integer part
    if (str[i] != '.') {
      if (TxtCharIsDigit(str[i]))
        result = result * 10.0 + (str[i] - 0x30);
      else
        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 (TxtCharIsDigit(str[i]))
        fractPart = fractPart / 10.0 + (str[i] - 0x30);
      else
        return 0.0;
    }
    result += fractPart / 10.0;
  }

   return result * sign;    // correcting the sign
}

----------------------------------------------------------------------------
------

Cheers,
Ralph


"David Orriss Jr" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Ralph Curtis wrote:
> > Seems that most people use MathLib for intensive floating point work.
Also
> > provides trig functions. You might also search the archives for
> > DoubleToString and StringToDouble algorithms that people have posted a
> > number of times.
> >
> >
>
> I managed to find a version of DoubleToString in the archives, but not
> StringToDouble...  oh well.. Thanks.
>
> --
> DavidO
>
> My blog: http://mywebpages.comcast.net/daorriss/
> My PalmOS apps:
>
http://www.palmgear.com/index.cfm?fuseaction=software.developer&userID=82264
6423
>
>



-- 
For information on using the Palm Developer Forums, or to unsubscribe, please 
see http://www.palmos.com/dev/support/forums/

Reply via email to