(defn as-currency
"Money amounts are transmitted as \"$2.44\".
Parse this and return a numeric type."
[currency-amount]
;; strip off the leading "$"
(let [stripped (subs currency-amount 1)
_ (println "stripped" currency-amount "->" stripped)
decimal (bigdec stripped)
_ (println "bigdec" stripped "->" decimal)]
decimal))
;;;; Re-evaluating the sample calculation gives us this:
;; stripped $1200.20 -> 1200.20
;; bigdec 1200.20 -> 1200.20M
;; stripped $0.03 -> 0.03
;; bigdec 0.03 -> 0.03M
;; stripped $120.00 -> 120.00
;; bigdec 120.00 -> 120.00M
;; stripped $5.99 -> 5.99
;; bigdec 5.99 -> 5.99M
;; stripped $20 -> 20
;; bigdec 20 -> 20M
;; stripped $12.33 -> 12.33
;; bigdec 12.33 -> 12.33M
;; stripped -$4.50 -> $4.50
;; NumberFormatException java.math.BigDecimal.<init> (BigDecimal.java:494)