Hello friend,
I was facing the same problem a long ago. Though Math.lib provides methods
for float value calculation there is no specific API for converting float to
string.
I had received the following custom function from palm device forum. Use it
to solve the problem.
Regards
Shoeb
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);
}
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of Syed
Najeebullah Hussaini
Sent: Monday, March 25, 2002 5:52 PM
To: Palm Developer Forum
Subject: RE: Float Formatting
There is one library available for PalmOS to do the Floating point
manipulation. It is called as MathLib... U can get it and make ur work
done..
Syed
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of
V...i...K...a...S
Sent: Monday, March 25, 2002 5:16 PM
To: Palm Developer Forum
Subject: Float Formatting
Hi all,
We have the following requirement:-
I have multiplied 2 floating values and converted the result into a string
.The string value is
not formatted properly
i.e I want :- "12.34"
i am getting :- "1.234000000e01"
Does the palm OS provide an functions for getting what I want or
i shall have to manually do the parsing n convert it.
Thanx in advance...
Vikas
--
For information on using the Palm Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/support/forums/
--
For information on using the Palm Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/support/forums/
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/support/forums/