If "%f" with no modifiers is passed to a standard sprintf function, it
places 6 digits after the decimal point.  So the values that sprintf
produced for me were (using Unix):

12.345000
123.400002
1234.560059
1234.500000
12345.000000

Some rounding errors were also introduced.

I had a case about a month ago where I needed to print numbers in floating
point format, and did not want the scientific format that FlptFToA produced.
Maybe there is a better solution, but since I knew I wanted four digits
after the decimal, I did the following:
- multiply the float by 10,000  (moves decimal place 4 places to right)
- typecast the value to a long int
- for each digit of the resulting integer
    - pick off last digit using a mod 10 (num %% 10)
    - add to string
    - after four digits added to string, insert a decimal point into string
    - divide number by 10
- reverse the string

I know it is kind of brute force, but it works.

Mike


-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of David
Leland
Sent: Thursday, March 01, 2001 10:00 AM
To: Palm Developer Forum
Subject: Re: Determining number of decimal positions


I was afraid that might be the case.  So let me ask hypothetically, if the
Palm OS supported the "%f" formatting modifier on the StrPrintF function
(e.g.  returnedString = StrPrintF("%f", floatValue);), what value would be
placed into returnedString if the float values were:
12.3450
123.400
1234.56
1234.50
12345.00

Dave

Mike Walters <[EMAIL PROTECTED]> wrote in message news:41279@palm-dev-forum...
>
> 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/


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

Reply via email to