Skip to content

Instantly share code, notes, and snippets.

@maxp
Created February 8, 2022 18:12
Show Gist options
  • Save maxp/7a7db5057e7f3c3cd67728e8377342a2 to your computer and use it in GitHub Desktop.
Save maxp/7a7db5057e7f3c3cd67728e8377342a2 to your computer and use it in GitHub Desktop.
grouped decimal number formatter
(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