;; From e.g. https://github.com/scottjad/uteal
(defmacro dlet
"let with inspected bindings"
[bindings & body]
`(let [~@(mapcat (fn [[n v]]
(if (or (vector? n) (map? n))
[n v]
[n v \'_ `(println (name \'~n) ":" ~v)]))
(partition 2 bindings))]
~@body))
(defn as-currency
"Money amounts are transmitted as \\"$2.44\\".
Parse this and return a numeric type."
[currency-amount]
;; strip off the leading "$"
(dlet [stripped (subs currency-amount 1)
decimal (bigdec stripped)]
decimal))
;;;; Re-evaluating the sample calculation gives us this:
;; stripped : 1200.20
;; decimal : 1200.20M
;; stripped : 0.03
;; decimal : 0.03M
;; stripped : 120.00
;; decimal : 120.00M
;; stripped : 5.99
;; decimal : 5.99M
;; stripped : 20
;; decimal : 20M
;; stripped : 12.33
;; decimal : 12.33M
;; stripped : $4.50
;; NumberFormatException java.math.BigDecimal.<init> (BigDecimal.java:494)