1
0
Fork 0
cl-sites/HyperSpec-7-0/HyperSpec/Body/22_ck.htm
2024-04-01 10:24:07 +02:00

128 lines
6.8 KiB
HTML

<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
<HTML>
<HEAD>
<TITLE>CLHS: Section 22.3.11</TITLE>
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
<LINK REL=TOP HREF="../Front/index.htm">
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
<LINK REL=PREV HREF="22_cjd.htm">
<LINK REL=UP HREF="22_c.htm">
<LINK REL=NEXT HREF="22_cl.htm">
</HEAD>
<BODY>
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="22_cjd.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="22_c.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="22_cl.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<H2>
22.3.11 Examples of FORMAT</H2> <P>
<PRE>
(format nil &quot;foo&quot;) =&gt; &quot;foo&quot;
(setq x 5) =&gt; 5
(format nil &quot;The answer is ~D.&quot; x) =&gt; &quot;The answer is 5.&quot;
(format nil &quot;The answer is ~3D.&quot; x) =&gt; &quot;The answer is 5.&quot;
(format nil &quot;The answer is ~3,'0D.&quot; x) =&gt; &quot;The answer is 005.&quot;
(format nil &quot;The answer is ~:D.&quot; (expt 47 x))
=&gt; &quot;The answer is 229,345,007.&quot;
(setq y &quot;elephant&quot;) =&gt; &quot;elephant&quot;
(format nil &quot;Look at the ~A!&quot; y) =&gt; &quot;Look at the elephant!&quot;
(setq n 3) =&gt; 3
(format nil &quot;~D item~:P found.&quot; n) =&gt; &quot;3 items found.&quot;
(format nil &quot;~R dog~:[s are~; is~] here.&quot; n (= n 1))
=&gt; &quot;three dogs are here.&quot;
(format nil &quot;~R dog~:*~[s are~; is~:;s are~] here.&quot; n)
=&gt; &quot;three dogs are here.&quot;
(format nil &quot;Here ~[are~;is~:;are~] ~:*~R pupp~:@P.&quot; n)
=&gt; &quot;Here are three puppies.&quot;
</PRE>
</TT>
<PRE>
(defun foo (x)
(format nil &quot;~6,2F|~6,2,1,'*F|~6,2,,'?F|~6F|~,2F|~F&quot;
x x x x x x)) =&gt; FOO
(foo 3.14159) =&gt; &quot; 3.14| 31.42| 3.14|3.1416|3.14|3.14159&quot;
(foo -3.14159) =&gt; &quot; -3.14|-31.42| -3.14|-3.142|-3.14|-3.14159&quot;
(foo 100.0) =&gt; &quot;100.00|******|100.00| 100.0|100.00|100.0&quot;
(foo 1234.0) =&gt; &quot;1234.00|******|??????|1234.0|1234.00|1234.0&quot;
(foo 0.006) =&gt; &quot; 0.01| 0.06| 0.01| 0.006|0.01|0.006&quot;
</PRE>
</TT>
<PRE>
(defun foo (x)
(format nil
&quot;~9,2,1,,'*E|~10,3,2,2,'?,,'$E|~
~9,3,2,-2,'%@E|~9,2E&quot;
x x x x))
(foo 3.14159) =&gt; &quot; 3.14E+0| 31.42$-01|+.003E+03| 3.14E+0&quot;
(foo -3.14159) =&gt; &quot; -3.14E+0|-31.42$-01|-.003E+03| -3.14E+0&quot;
(foo 1100.0) =&gt; &quot; 1.10E+3| 11.00$+02|+.001E+06| 1.10E+3&quot;
(foo 1100.0L0) =&gt; &quot; 1.10L+3| 11.00$+02|+.001L+06| 1.10L+3&quot;
(foo 1.1E13) =&gt; &quot;*********| 11.00$+12|+.001E+16| 1.10E+13&quot;
(foo 1.1L120) =&gt; &quot;*********|??????????|%%%%%%%%%|1.10L+120&quot;
(foo 1.1L1200) =&gt; &quot;*********|??????????|%%%%%%%%%|1.10L+1200&quot;
</PRE>
</TT> As an example of the effects of varying the scale factor, the code <P>
<PRE>
(dotimes (k 13)
(format t &quot;~%Scale factor ~2D: |~13,6,2,VE|&quot;
(- k 5) (- k 5) 3.14159))
</PRE>
</TT> produces the following output: <P>
<PRE>
Scale factor -5: | 0.000003E+06|
Scale factor -4: | 0.000031E+05|
Scale factor -3: | 0.000314E+04|
Scale factor -2: | 0.003142E+03|
Scale factor -1: | 0.031416E+02|
Scale factor 0: | 0.314159E+01|
Scale factor 1: | 3.141590E+00|
Scale factor 2: | 31.41590E-01|
Scale factor 3: | 314.1590E-02|
Scale factor 4: | 3141.590E-03|
Scale factor 5: | 31415.90E-04|
Scale factor 6: | 314159.0E-05|
Scale factor 7: | 3141590.E-06|
</PRE>
</TT> <P>
<PRE>
(defun foo (x)
(format nil &quot;~9,2,1,,'*G|~9,3,2,3,'?,,'$G|~9,3,2,0,'%G|~9,2G&quot;
x x x x))
(foo 0.0314159) =&gt; &quot; 3.14E-2|314.2$-04|0.314E-01| 3.14E-2&quot;
(foo 0.314159) =&gt; &quot; 0.31 |0.314 |0.314 | 0.31 &quot;
(foo 3.14159) =&gt; &quot; 3.1 | 3.14 | 3.14 | 3.1 &quot;
(foo 31.4159) =&gt; &quot; 31. | 31.4 | 31.4 | 31. &quot;
(foo 314.159) =&gt; &quot; 3.14E+2| 314. | 314. | 3.14E+2&quot;
(foo 3141.59) =&gt; &quot; 3.14E+3|314.2$+01|0.314E+04| 3.14E+3&quot;
(foo 3141.59L0) =&gt; &quot; 3.14L+3|314.2$+01|0.314L+04| 3.14L+3&quot;
(foo 3.14E12) =&gt; &quot;*********|314.0$+10|0.314E+13| 3.14E+12&quot;
(foo 3.14L120) =&gt; &quot;*********|?????????|%%%%%%%%%|3.14L+120&quot;
(foo 3.14L1200) =&gt; &quot;*********|?????????|%%%%%%%%%|3.14L+1200&quot;
</PRE>
</TT> <P>
<PRE>
(format nil &quot;~10&lt;foo~;bar~&gt;&quot;) =&gt; &quot;foo bar&quot;
(format nil &quot;~10:&lt;foo~;bar~&gt;&quot;) =&gt; &quot; foo bar&quot;
(format nil &quot;~10&lt;foobar~&gt;&quot;) =&gt; &quot; foobar&quot;
(format nil &quot;~10:&lt;foobar~&gt;&quot;) =&gt; &quot; foobar&quot;
(format nil &quot;~10:@&lt;foo~;bar~&gt;&quot;) =&gt; &quot; foo bar &quot;
(format nil &quot;~10@&lt;foobar~&gt;&quot;) =&gt; &quot;foobar &quot;
(format nil &quot;~10:@&lt;foobar~&gt;&quot;) =&gt; &quot; foobar &quot;
</PRE>
</TT> <P>
<PRE>
(FORMAT NIL &quot;Written to ~A.&quot; #P&quot;foo.bin&quot;)
=&gt; &quot;Written to foo.bin.&quot;
</PRE>
</TT> <P>
<P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issue</A>, <I>not part of the specification</I>, applies to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss260.htm">PATHNAME-PRINT-READ:SHARPSIGN-P</A><P></UL><HR>
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
</BODY>
</HTML>