在常见的lisp中,如何格式化浮点并指定分组,组字符和小数分隔符char

Par*_*ife 5 common-lisp

假设我有浮点数 1234.9

我想将其格式化为 1.234,90

是否有格式指令组合?~D,可以处理分组和组char,只处理整数.~F根本不处理分组.据我所知,没有人可以将小数点更改.,

我看到的唯一解决方案是使用~D整数部分数字分组并将其与,小数部分连接.有更好的想法吗?

Ren*_*nzo 5

As the comment of jkiiski suggests, you could use the ~/func/ directive.

This is just an example, you can elaborate more with the function:

CL-USER> (defun q(stream arg &rest args)
           (declare (ignore args))
           (format stream 
                   "~,,'.,:D,~a" 
                   (truncate arg)
                   (let ((float-string (format nil "~f" arg)))
                     (subseq float-string (1+ (position #\. float-string))))))
Q
CL-USER> (format t "~/q/~%" 1024.36)
1.024,36
NIL
CL-USER> (format t "~/q/~%" -1024.36)
-1.024,36
NIL
Run Code Online (Sandbox Code Playgroud)

Edited

The first version had round, which is wrong, truncate is the right operator to use.


Jos*_*lor 5

您可以定义一个用波浪号斜线调用的函数,大多数其他答案已经完成,但为了获得类似于 ~F 的输出,但注入逗号字符,并替换小数点,我认为它是最好调用get~F产生的输出,然后修改并写入字符串。这里有一种方法可以做到这一点,使用实用程序注入逗号以指定的间隔向字符串添加逗号字符。这是指令函数:

(defun print-float (stream arg colonp atp
                    &optional
                      (point-char #\.)
                      (comma-char #\,)
                      (comma-interval 3))
  "A function for printing floating point numbers, with an interface
suitable for use with the tilde-slash FORMAT directive.  The full form
is 

    ~point-char,comma-char,comma-interval/print-float/

The point-char is used in place of the decimal point, and defaults to
#\\.  If : is specified, then the whole part of the number will be
grouped in the same manner as ~D, using COMMA-CHAR and COMMA-INTERVAL.
If @ is specified, then the sign is always printed."
  (let* ((sign (if (minusp arg) "-" (if (and atp (plusp arg)) "+" "")))
         (output (format nil "~F" arg))
         (point (position #\. output :test 'char=))
         (whole (subseq output (if (minusp arg) 1 0) point))
         (fractional (subseq output (1+ point))))
    (when colonp
      (setf whole (inject-comma whole comma-char comma-interval)))
    (format stream "~A~A~C~A"
            sign whole point-char fractional)))
Run Code Online (Sandbox Code Playgroud)

这里有些例子:

(progn 
  ;; with @ (for sign) and : (for grouping)
  (format t "~','.2@:/print-float/ ~%" 12345.6789) ;=> +1.23.45,679

  ;; with no @ (no sign) and : (for grouping)
  (format t "~'.'_3:/print-float/ ~%" 12345.678)   ;=>  12_345.678

  ;; no @ (but sign, since negative) and : (for grouping)
  (format t "~'.'_3:/print-float/ ~%" -12345.678)  ;=> -12_345.678

  ;; no @ (no sign) and no : (no grouping)
  (format t "~'.'_3@/print-float/ ~%" 12345.678))  ;=> +12345.678 (no :)
Run Code Online (Sandbox Code Playgroud)

以下是来自coredump- 的答案的示例,它实际上帮助我发现了一个带有负数的错误:

CL-USER> (loop for i in '(1034.34 -223.12 -10.0 10.0 14 324 1020231)
            do (format t "~','.:/print-float/~%" i))
1.034,34
-223,12
-10,0
10,0
14,0
324,0
1.020.231,0
NIL
Run Code Online (Sandbox Code Playgroud)

这是inject-comma,有一些例子:

(defun inject-comma (string comma-char comma-interval)
  (let* ((len (length string))
         (offset (mod len comma-interval)))
    (with-output-to-string (out)
      (write-string string out :start 0 :end offset)
      (do ((i offset (+ i comma-interval)))
          ((>= i len))
        (unless (zerop i)
          (write-char comma-char out))
        (write-string string out :start i :end (+ i comma-interval))))))
Run Code Online (Sandbox Code Playgroud)
(inject-comma "1234567" #\, 3)
;;=> "1,234,567"

(inject-comma "1234567" #\. 2)
;;=> "1.23.45.67"
Run Code Online (Sandbox Code Playgroud)