Format string for zero padding with decimal point in front?

Is there a format string for zero padding with decimal in front?

(format nil ? 5) => "+0005"
(format nil ? -5) => "-0005"

Closest I found

(format nil "~4,'0@d" 5) => "00+5"
(format nil "~4,'0@d" -5) => "00-5"
+4
source share
1 answer

This is a draft of a custom printer function:

(defun signed-padding (stream data colonp atsignp &optional (padding 0))
  (declare (ignore colonp atsignp))
  (format stream "~:[+~;-~]~v,'0d" (minusp data) padding (abs data)))

... and an example:

(values
 (format nil "~v/signed-padding/" 20 330)
 (format nil "~5/signed-padding/" -4))

"+00000000000000000330"
"-00004"

You can probably add additional checks and parameters.

+6
source

Source: https://habr.com/ru/post/1628707/


All Articles