buatdolar


SUBMITTED BY: buatdolar

DATE: Feb. 12, 2018, 9:06 p.m.

FORMAT: Text only

SIZE: 866 Bytes

HITS: 584

  1. (defn as-currency
  2. "Money amounts are transmitted as \"$2.44\".
  3. Parse this and return a numeric type."
  4. [currency-amount]
  5. ;; strip off the leading "$"
  6. (let [stripped (subs currency-amount 1)
  7. _ (println "stripped" currency-amount "->" stripped)
  8. decimal (bigdec stripped)
  9. _ (println "bigdec" stripped "->" decimal)]
  10. decimal))
  11. ;;;; Re-evaluating the sample calculation gives us this:
  12. ;; stripped $1200.20 -> 1200.20
  13. ;; bigdec 1200.20 -> 1200.20M
  14. ;; stripped $0.03 -> 0.03
  15. ;; bigdec 0.03 -> 0.03M
  16. ;; stripped $120.00 -> 120.00
  17. ;; bigdec 120.00 -> 120.00M
  18. ;; stripped $5.99 -> 5.99
  19. ;; bigdec 5.99 -> 5.99M
  20. ;; stripped $20 -> 20
  21. ;; bigdec 20 -> 20M
  22. ;; stripped $12.33 -> 12.33
  23. ;; bigdec 12.33 -> 12.33M
  24. ;; stripped -$4.50 -> $4.50
  25. ;; NumberFormatException java.math.BigDecimal.<init> (BigDecimal.java:494)

comments powered by Disqus