From: Madhu
Subject: Re: how to suppress tailing zeros with FORMAT ~F
Date: 
Message-ID: <m34oztqspv.fsf@moon.robolove.meer.net>
* jimka <····································@d36g2000prf.googlegroups.com> :
Wrote on Tue, 20 Jan 2009 08:23:31 -0800 (PST):

| (format nil "~4,3F" some-number) will print eactly 3 digits after the
| decimal, if some-number is 3.2, then it prints 3.200.  How can i tell
| format to supress the trailing 0's?  anybody know?  obviously i could
| as format to print to a string, then post-process the string... but
| maybe there's an easier way.

Or you could pre-process the number and use PRIN1.  

(defun round-to-accuracy (value digits-after-decimal-point)
  "Internal. Round VALUE to DIGITS decimal places."
  (declare (type (integer 0 10) digits-after-decimal-point))
  (let ((precision (expt 10 digits-after-decimal-point)))
    (if (integerp value)
        value
        (coerce
         (/ (round (* precision value)) precision)
         (type-of value)))))

I use this in some cases where I require the output of my program to be
identical to the output of [non lisp] program.

note in the case of FORMAT's float directives it makes sense to not do
this because when you are dealing with CL floats, depending on how your
3.2 was represented, rounding and printing them could possibly end up
with 3.199 (say) [ANS section 22.3.3.1]

--
Madhu