mad*_*tap 3 clojure common-lisp string-formatting
我正在尝试cl-format用来格式化钱.我想要(f 12345.555) ;=> "12,345.56".我得到格式字符串的小数,"~$"我得到逗号分隔符"~:D".我如何组合它们?
使用Common Lisp,我建议使用cl-l10n支持locales和定义的~N.或者,您可以自己动手:
(defun money (stream number colonp atsignp &optional (decimal-places 2))
(multiple-value-bind (integral decimal) (truncate number)
(format stream
(concatenate 'string
"~"
(and colonp ":")
(and atsignp "@")
"D"
"~0,vf")
integral
decimal-places
(abs decimal))))
(setf *read-default-float-format* 'double-float)
(format nil "~2:@/money/" 123456789.123456789)
=> "+123,456,789.12"
Run Code Online (Sandbox Code Playgroud)
现在,对于Clojure来说,它似乎~/尚未得到支持cl-format,因此您无法直接复制上述代码.使用Java库可能更快(参见例如这个问题或 另一个问题).