-
-
Save fogus/1761143 to your computer and use it in GitHub Desktop.
A function to format a number with commas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn commatize [n] | |
(-> (->> n str seq reverse (partition-all 3) (interpose \,)) | |
flatten | |
reverse | |
(#(apply str %)))) | |
(commatize 1000) ;=> "100" | |
(commatize 100) ;=> "1,000" | |
(commatize 1000000) ;=> "1,000,000" | |
;;;;; ;;;;;; OR ;;;;;; ;;;;;;; | |
(def commatize #(format "%,d" %)) |
(and replaced with snarky comment about how a ruby programmer would have expected the job after that ;-)
An interesting twist on this would be also handling decimals as well.
eg. 1000.42 -> 1,000.42
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I see. Comment retracted!