Skip to content

Instantly share code, notes, and snippets.

@quoll
Last active January 6, 2022 20:07
Show Gist options
  • Save quoll/f0973d878a9339220cdd3ebafef229d0 to your computer and use it in GitHub Desktop.
Save quoll/f0973d878a9339220cdd3ebafef229d0 to your computer and use it in GitHub Desktop.
(require '[clojure.string :as string])
(defn f
[n]
(let [s (str n)]
(js/parseFloat
(if-let [[[_ a b]] (re-seq #"(\d*)\.(\d*)" (if (string/starts-with? s "0") (subs s 1) s))]
(let [al (- (count a) 3)]
(if (> al 0)
(str (subs a 0 al) "." (subs a al) b)
(str (apply str "0." (repeat (- al) "0")) (when-not (= "0" a) a) b)))
(let [nl (- (count s) 3)]
(str (subs s 0 nl) "." (subs s nl)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment