Okay, I fixed the problem... it was pretty stupid. I should have caught it
earlier! It was staring me right in the face :-)
Here is the working version for all who is / isn't interested. lol
If any newbie is interested in the code, and needs the rest of the
additional segments, just lemme know.
CharPtr AddDecimals(CharPtr str1, CharPtr str2, UInt places)
{
// Need to integrate limited decimal places in string
// Rounding as well...
decNum dec1; // Decimal structures
decNum dec2;
CharPtr lstr; // Strings
CharPtr strResult;
long int result; // Addition result
long int temp; // used to find length of result
int counter = 1; // Length of int
int x, i = 0, j = 0; // Counters
Boolean cont = true;
// Get Decimal structure from Strings
GetDecFromString(str1, &dec1);
GetDecFromString(str2, &dec2);
// Make numbers same length
if(dec1.dec > dec2.dec)
for(x = 0; x < (dec1.dec - dec2.dec); ++x)
dec2.num = dec2.num * 10;
else if(dec1.dec < dec2.dec)
for(x = 0; x < (dec2.dec - dec1.dec); ++x)
dec1.num = dec1.num * 10;
// Get the sum
result = dec1.num + dec2.num;
// Count # of places it occupies
for(temp = result; temp > 0; temp = temp / 10)
counter++;
// Allocate memory for local string
lstr = (CharPtr)MemPtrNew(counter);
strResult = (CharPtr)MemPtrNew(counter + 1);
// Covert UInt to String
StrIToA(lstr, result);
// Subtract length result by largest dec value
if(dec1.dec > dec2.dec)
counter = counter - dec1.dec;
else
counter = counter - dec2.dec;
// Copy String and insert decimal
while(lstr[i] != '\0')
{
if((i == (counter - 1)) && (cont == true))
{
strResult[j] = '.';
j++;
cont = false;
}
else
{
strResult[j] = lstr[i];
i++;
j++;
}
}
/************ FORGOT TO TERMINATE STRING :-) **********/
strResult[j] = '\0';
// Free memory
MemPtrFree(lstr);
MemPtrFree(strResult);
return(strResult);
}
--
Tim Astle
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palm.com/devzone/mailinglists.html