Created
February 8, 2022 18:12
-
-
Save maxp/7a7db5057e7f3c3cd67728e8377342a2 to your computer and use it in GitHub Desktop.
grouped decimal number formatter
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 grouped-dec [decimal-number] | |
;; NOTE: handle sign separately | |
(when decimal-number | |
(let [s (str decimal-number) | |
[deci fract] (str/split s #"\.") | |
grouped | |
(->> deci | |
(reverse) | |
(partition 3 3 nil) | |
(map #(apply str %)) | |
(interpose "\u202f") | |
(apply str) | |
(reverse) | |
(apply str))] | |
(apply str grouped "." (take 2 (str fract "00")))))) | |
(comment | |
(grouped-dec (bigdec 12345678.02)) | |
;; => "12 345 678.02" | |
(grouped-dec (bigdec 0.0)) | |
;; => "0.00" | |
(grouped-dec 0) | |
;; => "0.00" | |
(grouped-dec 1000) | |
;; => "1 000.00" | |
,) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment