Skip to content

Instantly share code, notes, and snippets.

@mihi-tr
Created August 22, 2014 15:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mihi-tr/28b4d176dba7d33057e6 to your computer and use it in GitHub Desktop.
Save mihi-tr/28b4d176dba7d33057e6 to your computer and use it in GitHub Desktop.
Zero Pad a number in Clojure
(defn zp "Zero Pad numbers - takes a number and the length to pad to as arguments"
[n c]
(loop [s (str n)]
(if (< (.length s) c)
(recur (str "0" s))
s)))
@pavanivellal
Copy link

pavanivellal commented Feb 13, 2018

(defn zp "Zero Pad numbers - takes a number and the length to pad to as arguments"
  [n &[c]]
  (if c
    (as-> c val
          (str "%0" val "d") 
          (format val n))
    (str n)))

@abogoyavlensky
Copy link

abogoyavlensky commented Apr 4, 2021

(defn zp
  "Zero Pad numbers - takes a number and the length to pad to as arguments"
  [n c]
  (format (str "%0" c "d") n))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment