Elisp: Format String
Format
format-
(format STRING &rest OBJECTS)Convert lisp objects into a string. It takes a input string, and several other arguments of lisp objects, and output a string.
- The format control string may contain placeholders
%c. The placeholders are replaced by argument to format. - Different placeholders means convert the argument to a string in particular way.
(format "Name: %s, age: %d" "jane" 25) ;; "Name: jane, age: 25" ;; decimal to hex (format "%x" 10) ;; "a" ;; hexadecimal to decimal (format "%d" #xa) ;; "10" - The format control string may contain placeholders
Format Template Spec
%s-
string. Converted by
princ. 〔see Elisp: Print, Output〕 %d- number to decimal.
%o- number to octal notation.
%x- number to hexadecimal notation.
%X-
like
%x, but uses upper case. %e- number to exponential notation.
%f- number to decimal-point notation.
%g- number to exponential notation or decimal-point notation, whichever uses fewer characters.
%c- integer (Codepoint (Character ID)) to character.
%S-
any lisp object to lisp syntax (using
prin1). 〔see Elisp: Print, Output〕
Padding
;; padding decimal, 4 digits (format "%04d" 100) ;; "0100" ;; padding hexadecimal, 4 digits (format "%04x" 100) ;; "0064"
Adding Sign
;; always add sign (format "%+d" 9) ;; "+9" (format "%+d" -9) "-9" ;; s------------------------------ ;; add sign, and padding (format "%+04d" 10) ;; "+010" (format "%+04d" -10) ;; "-010"