Use GetStringFromDouble. It's been posted here several times, and beleive
me, it works 100%. I use it frequently. Here it is again, in case you
missed it, along with it's partner GetDoubleFromString.
// ==================================
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;
}
}
// ==================================
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;
}
// ==================================
Alan
"Brad Figler" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
>
> > (long) (x + 0.5)
> >
> > I thought everyone learnt this trick in primary school? Or at home,
> > while programming in BASIC as a child?
>
> It is funny that you say that, because now that I see that, I cannot
believe
> I did not remember it. Thanks for reminding me. Actually, what I did was
> re-write it from scratch using the float manager to return the base 10
info
> (mantissa, sign, and exponent) and constructed the string that way. It
> works great.
>
> ~Brad
>
>
>
>
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/support/forums/