Subject: Re: help with format
From: rpw3@rpw3.org (Rob Warnock)
Date: Tue, 10 Apr 2007 04:41:32 -0500
Newsgroups: comp.lang.lisp
Message-ID: <vsSdnRCDIINRxobbnZ2dnUVZ_ternZ2d@speakeasy.net>
Pascal Bourguignon  <pjb@informatimago.com> wrote:
+---------------
| getasso@gmail.com writes:
| > how to i get comma seperated integer/float point with "Format"
| > example 10,000.5 - > 10,000.5
| > what other arguments other than ':D' should be included to cause that
| > effect.
| >   (format t "~:D,etc,etc" 10000.5)
| 
| Use the monetary format ~$, which is useful also on non monetary values. 
+---------------

As far as I know [and I searched CLHS "22.3.3.4 Tilde Dollarsign:
Monetary Floating-Point" pretty closely], monetary format doesn't
provide for printing the integer portion in comma-separated format,
nor do any of the other floating-point formats (~E, ~F, ~G).

Conversely, if you give any of the commachar-printing integer formats
(~R, ~D, ~B, ~O, ~X) a floating-point number, it uses ~F (?) instead,
and you lose the "commachar" printing.

So I think the only way to do what "getasso" wanted is something ugly
like this:

    > (format nil "~{~:D~3,2F~}" (multiple-value-list (floor 10000.5)))

    "10,000.50"
    > 

or less concisely but also with less consing:

    > (multiple-value-bind (i f)
	  (floor 10000.5)
        (format nil "~:D~3,2F" i f))

    "10,000.50"
    > 

Notice that these use the special case of "w=d+1" mentioned in
CLHS "22.3.3.1 Tilde F: Fixed-Format Floating-Point" to suppress
the leading zero of the fraction part:

    Leading zeros are not permitted, except that a single zero digit
    is output before the decimal point if the printed value is less
    than one, and this single zero digit is not output at all if w=d+1.


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607