Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pad an argument with zeroes using the lisp format function?

Tags:

common-lisp

I am playing around with lisp's format function, but I have hit a snag because although I can get it to write the list of numbers aligned nicely, I can't seem to get it to zero pad it:

(defun inc (a) (+ 1 a))
(dotimes (i 10)
  (format t "~3@:D ~:*~R~%" (inc i)))

This produces the following output:

+1: one
+2: two
+3: three
+4: four
+5: five
+6: six
+7: seven
+8: eight
+9: nine
+10: ten

Does anybody know how to get it to be zero-padded?

like image 484
tomjen Avatar asked Dec 29 '08 14:12

tomjen


1 Answers

Example lifted from the PCL chapter on FORMAT:

(format nil "~12d" 1000000)    ==> "     1000000"
(format nil "~12,'0d" 1000000) ==> "000001000000"
like image 185
J Cooper Avatar answered Dec 31 '22 18:12

J Cooper