By "number of decimal places" do you mean the number before the decimal, the
number after, or both?
1) For the number before the decimal place: I don't know of a built-in
function, but you could use a small piece of code like the following to
count the number of places:
int CountDigits (float your_val)
{
int count;
count = 0;
while (your_val > 1.0)
{
count ++;
your_val /= 10.0;
}
return count;
}
2) For the number of digits after the decimal, the answer really can't be
determined (do you count all of the trailing zeros, worry arount floating
point rounding, etc). So the easiest thing to do is just select a value to
use in displaying your number, then truncate off any trailing zeros that you
don't want.
3) For the total number of digits, you could probably use a combination of
the two methods above.
Of course, for any kind of problem, you are going to find almost as many
solutions as you have responses. An without having the M&R book in front of
me, I'm not sure the value that it is expecting to be passed.
Hope this helps,
Mike Walters
Rose Software
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of David
Leland
Sent: Thursday, March 01, 2001 9:28 AM
To: Palm Developer Forum
Subject: Determining number of decimal positions
I have a function that is being passed a float value. In my function, I
will be formatting that float value to a string (e.g. 1234.56 to 1,234.56)
and returning it. My problem is that I do not know the number of decimal
places being passed. The float value could be 123.456 or it could be
1234.56 or 12.3456. Is there a way I can determine how many decimal
positions that float value has?
"Advanced Palm Programming" by Mann & Rischpater has an excellent routine
for formatting a float value to a string but unfortunately, it expects the
calling function to tell it how many decimal places to format. My App is a
Plug-in to another Palm App so I don't have the luxury of knowing upfront
how many decimal places the float value has.
Thanks,
Dave
--
For information on using the Palm Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/tech/support/forums/
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/