Hello Luc,

Why not just modify one of the available routines? Adding a comma
every 3 digits shouldn't be a complex modification. I was going to
suggest using the widely used GetStringFromDouble() and
GetDoubleFromString() functions from the archives, but it seems they
are no longer available. Since I couldn't find them, I figured I'd
post them so that others can benefit. I believe these functions are
unchanged from the original posting (aside from formatting).
Unfortunately I don't remember who the original author was, perhaps
someone else can fill in that detail.

Adrien.

//********************************************************************
// GetStringFromDouble()
//********************************************************************
//
// Gets string from double, and rounds to the appropriate number of
// decimal points.
//
// Taken from Palm Dev Forum
//
//********************************************************************

void GetStringFromDouble(Char* str, double dblNum, Int16 numFractDigits)
{
  double flpIP, zeros, round;
  Int32 remainder, longNumber;
  Int16 i, strLen;
  Char buffer[16];

  str[0] = 0;

  if (dblNum < 0.0D)
  {
    dblNum = -dblNum;
    StrCat(str, "-");
  }

  zeros = 1.0D;
  for (i = 0; i < numFractDigits; i++)
  {
    zeros *= 10.0D;
  }
  round = 0.5D / zeros;

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

  StrIToA(buffer, (Int32) flpIP);
  StrCat(str, buffer);
  strLen = (Int16) StrLen(str);
  StrCat(str, "."); // put in the decimal point and terminate the string
  str[numFractDigits + strLen + 1] = '\0';
  longNumber = (Int32) (dblNum * zeros); // fractional part

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

//********************************************************************
// GetDoubleFromString()
//********************************************************************
//
// Extracts a double from a string.
//
// Taken from Palm Dev Forum
//
//********************************************************************

double GetDoubleFromString(Char* s)
{
  #define DOUBLE_STRING_BUFFER_SIZE 12

  Char leftstr[DOUBLE_STRING_BUFFER_SIZE], rightstr[DOUBLE_STRING_BUFFER_SIZE];
  double leftval, rightval, result;
  Int16 j = 0, k = 0;
  Boolean isNegative = false;

  if (*s == '-')
  {
    isNegative = true;
    s++;
  }

  // strip off LHS of number
  for (j = 0; (s[j] != '\0') && (s[j] != '.') &&
       (j < DOUBLE_STRING_BUFFER_SIZE); j++)
  {
    leftstr[j] = s[j];
  }

  if (j == DOUBLE_STRING_BUFFER_SIZE) // ERROR - RETURN ZERO
  {
    result = 0.0D;
    return(result);
  }

  leftstr[j] = '\0'; // done, terminate it

  if (leftstr[0] == ' ')
  {
    leftstr[0] = '0';
  }

  // strip off RHS of number

  if (s[j] == '.')
  {
    j++;
  }

  while ((s[j] != '\0') && (k < DOUBLE_STRING_BUFFER_SIZE))
  {
    rightstr[k++] = s[j++]; // get RHS
  }

  if (k == DOUBLE_STRING_BUFFER_SIZE) // ERROR - RETURN ZERO
  {
    result = 0.0D;
    return(result);
  }

  rightstr[k] = '\0'; // done, terminate it

  // convert the two parts to floating point #s

  leftval = (double) StrAToI(leftstr);

  rightval = (double) StrAToI(rightstr);

  // convert RHS to proper fraction
  for (j = 1; j <= StrLen(rightstr); j++)
  {
    rightval /= 10.0D;
  }

  // combine and return results

  result = leftval + rightval;

  if (isNegative)
  {
    result *= -1.0D;
  }

  return(result);
}

Monday, June 6, 2005, 10:28:11 PM, you wrote:

LLB> PalmOS has a StrLocalizeNumber API that will convert a number formatted
LLB> as n,nnn.nn with proper thousands and decimal separators. But how do you
LLB> get that string in the first place? All double->ASCII routines I've seen
LLB> so far don't bother to add thousands separators.


LLB> --
LLB> Luc Le Blanc



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

Reply via email to