1: 基本格式化
(format destination control-string &rest format-arguments)
(输出目的 控制字符串 格式化参数)
@,冒号 --> 修饰符
~A,~D -->控制字符串
V,#,十进制数字 --> 前置参数 (关键是寻找到底是用一个子句)
~A/~S 都可以使用任何类型的格式化参数,~S试图输出生成为可被read读回来的形式,所以字符串包含在引号内
CL-USER> (format t "value is: ~S" "foo") value is: "foo"
CL-USER> (format t "value is: ~A" "foo") value is: foo
CL-USER> (format t "the value is: ~A" (list 1 2 3)) the value is: (1 2 3)
CL-USER> (format t "the value is: ~S" (list 1 2 3)) the value is: (1 2 3)
读取整数十进制的~d,有两位的前置变量,第一个参数指输出最小宽度,第二个指占位符字符
CL-USER> (format t "~d" 1000000) 1000000
CL-USER> (format t "~:d" 1000000) 1,000,000
CL-USER> (format t "~@:d" 1000000) +1,000,000
CL-USER> (format t "~12d" 1000000) 1000000
CL-USER> (format t "~12,'0d" 1000000)000001000000
CL-USER> (format t "~4,'0d-~2,'0d-~2,'0d" 2005 6 10) 2005-06-10
CL-USER> (format t "~x" 1000000) F4240
CL-USER> (format t "~o" 1000000) 3641100
CL-USER> (format t "~b" 1000000) 11110100001001000000
浮点共有四个指令:~f,~e,~g,~s,~f指令在数字特别大或特别小的时候允许使用科学计数法,~e指令在输出数字时总是使用科学计数法。
浮点指令~$ 等价于~,2f.如其名它是用于货币的,并且因为~5$中5表示小数点后位数,所以因为他可以有两个前置参数,第二个参数表控制十进制小数前所打印的最少位数.
CL-USER> (format t "~$" pi) 3.14
CL-USER> (format t "~5$" pi) 3.14159
CL-USER> (format t "~2,4$" pi) 0003.14
CL-USER> (format t "~f" pi) 3.141592653589793
CL-USER> (format t "~,4f" pi) 3.1416
CL-USER> (format t "~e" pi) 3.141592653589793d+0
CL-USER> (format t "~,4e" pi) 3.1416d+0
CL-USER> (format t "~2,4$" pi) 0003.14
转化为英文的数字表达形式
CL-USER> (format t "~r" 1234) one thousand two hundred thirty-four
CL-USER> (format t "~:r" 1234)one thousand two hundred thirty-fourth
格式化参数不为1就输出S,表示复数啦.
CL-USER> (format t "file~p" 1) file
CL-USER> (format t "file~p" 10)files
~(另一个表示英文文本也就是string的指令,他必须与~)对应,下面第一个是直接用~a格式输出,它完全没有变化,而这个指令可以实现控制大小写.@将第一个单词首字母大写,冒号所有单词首字母大写,@与冒号同时
使用所有字母都大写
CL-USER> (format t "~@a" "the Quick BROWN foX") the Quick BROWN foX
CL-USER> (format t "~(~a~)" "FOO") foo
CL-USER> (format t "~(~a~)" "the Quick BROWN foX") the quick brown fox
CL-USER> (format t "~@(~a~)" "the Quick BROWN foX") The quick brown fox
CL-USER> (format t "~:(~a~)" "the Quick BROWN foX") The Quick Brown Fox
CL-USER> (format t "~:@(~a~)" "the Quick BROWN foX")THE QUICK BROWN FOX
Format directives can take arguments. ~F, which is used for printing right-justified floating-point numbers, can take up to five:
1. The total number of characters to be printed. Defaults to the exact length of the number.
2. The number of digits to print after the decimal. Defaults to all of them.
3. The number of digits to shift the decimal point to the left (thereby effectively multiplying the number by 10). Defaults to none.
4. The character to print instead of the number if it is too long to fit in the space allowed by the first argument. If no character is specified, an over-long number will be printed using as much space as it needs.当数字太大的时候,第一个参数指定的位数不够用
5. The character to print to the left before the digits start. Defaults to a blank.
CL-USER> (format nil "~10,2,0,'*,' f" 26.21875)
" 26.22"
CL-USER> (format nil "~10,2,1,0,' f" 26.2187512434343434434434)
" 262.19"
CL-USER> (format nil "~10,2,1,'8,' f" 262187512434.343434434434)
"8888888888"
CL-USER> (format nil "~10,2,1,'a,' f" 262187512434.343434434434)
"aaaaaaaaaa"
CL-USER> (format t "~10,2,1,'a,' f" 262187512434.343434434434)
aaaaaaaaaa
NIL
the first argument
If we give t as the first argument, output is sent to * standard- output*.
If we give nil, format returns as a string what it would have printed.
CL-USER> (format t "~S ~A" "z" "z")
"z" z
NIL
CL-USER> (format nil "~S ~A" "z" "z")
"\"z\" z"
输出换行符
~% 总是产生换行符,可接受单个前置参数指定要产生的换行的个数。
~& 只在当前没有位于一行开始处时才产生换行,如果有前置参数会输出n-1或n个换行,取决与它是否在一行的开始处输出
~~ format输出波浪线,可以通过一个数字来参数化控制产生多少个波浪线
2: 条件格式化
~[对应格式~],在他们之间是有~;分割的句子。~[选取一个子句,然后让format处理,他一般有下面的形式决定到底是取那个句子。其中包含两个注意点:
1:千万别忘记写~]否则总是提示不能编译
2:最后一个子句分隔符是~:;表示最为默认的,如果没有这一个,下面100超界,将不会打印结果。
CL-USER> (format t "~[cero~;uno~;dos~:;mucho~]" 100)
mucho
CL-USER> (format t "~[cero~;uno~;dos~:;mucho~]" 2)
dos
CL-USER> (defparameter *list-etc*
"~#[none~;~a~;~a and ~a:;~a,~a~]~#[~; and ~a~:;, ~a, etc~].")
*LIST-ETC*
CL-USER> (format t *list-etc*) none.
CL-USER> (defparameter *list-etc*
"~#[none~;~a1~;~a and ~a2~:;~a,~a~3]~#[4~; and ~a5~:;, ~a, etc~].")
*LIST-ETC*
CL-USER> (format t *list-etc* 'a) A14. A.
CL-USER> (format t *list-etc* 'a 'b) A and B24. A and b.
CL-USER> (format t *list-etc* 'a 'b 'c) A,B and C5. A,B and C
CL-USER> (format t *list-etc* 'a 'b 'c 'd) A,B, C, etc. A,B, C, etc.
CL-USER> (format t *list-etc* 'a 'b 'c 'd 'e) A,B, C, etc. A,B, C, etc.
CL-USER> (format t "~:[fail~;pass~]" test-result)
3: 迭代
CL-USER> (format t "~{~a, ~}" (list 1 2 3))
1, 2, 3,
你也许感觉上面结果中3后面的逗号太碍眼了,有没有办法去掉呢?~^在一个~{体内,表当列表中没有元素剩余时,~^令迭代立即停止无需处理后面控制字符。
CL-USER> (format t "~{~a~^, ~}" (list 1 2 3))
1, 2, 3
~@{可以嵌入到~{ 或者~@{,并且未处理的元素,会一步步渗向底层,并且在最内层直至迭代结束。
CL-USER> (defparameter *english-list*
"~{~#[~;~A~;~A AND ~a~:;~@{~a~#[~;, and ~:;, ~]~}~]~}")
*ENGLISH-LIST*
CL-USER> (format nil *english-list* '(1 2 3 4))
"1, 2, 3, and 4"
CL-USER> (defparameter *english-list*
"~{~#[1~;~A2~;~A AND ~a3~:;~@{~a~#[4~;, 5and ~:;, 6~]~}~]~}")
*ENGLISH-LIST*
CL-USER> (format nil *english-list* '()) "" ""
CL-USER> (format nil *english-list* '(1)) "12" "1"
CL-USER> (format nil *english-list* '(1 2)) "1 AND 23" "1 and 2"
CL-USER> (format nil *english-list* '(1 2 3)) "1, 62, 5and 34" "1, 2, and 3"
CL-USER> (format nil *english-list* '(1 2 3 4)) "1, 62, 63, 5and 44" "1, 2, 3, and 4"