Yu-Gi-Oh! Deck Building and Card Inventory Management web interface written in Common Lisp, utilizing HTMX.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.1KB

  1. #|
  2. Money Stuff
  3. Storing Floating Point values in an SQL database is *BAD*!:
  4. CL-DECK-BUILDER2> (with-connection (db)
  5. (mito:retrieve-by-sql
  6. (sxql:select ((:as 0.005 :real)))))
  7. ((:REAL 0.004999999888241291d0))
  8. So I'm taking expert's advice and storing money as a fixed point integer with 100 divisions (cents).
  9. Deflate: Mito -> RDBMS
  10. Now with test suite!
  11. |#
  12. (in-package #:cl-deck-builder2.toolkit.money)
  13. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  14. (defgeneric currency-deflate (obj)
  15. (:documentation "CURRENCY-DEFLATE takes a NUMBER or a STRING and converts it into a fixed point number.
  16. ;; One Half Dollar / $0.50 / 50 Cents / These are all the same thing
  17. (CURRENCY-DEFLATE 1/2) => 50
  18. (CURRENCY-DEFLATE 0.50) => 50
  19. (CURRENCY-DEFLATE 0.5d0) => 50
  20. (CURRENCY-DEFLATE \"1/2\") => 50
  21. (CURRENCY-DEFLATE \"0.50\") => 50
  22. (CURRENCY-DEFLATE \"0.5d0\") => 50
  23. ;; One Dollar / $1.00 / 100 Cents / These are all the same thing
  24. (CURRENCY-DEFLATE 1) => 100
  25. (CURRENCY-DEFLATE 1.00) => 100
  26. (CURRENCY-DEFLATE 1.0d0) => 100
  27. (CURRENCY-DEFLATE \"1\") => 100
  28. (CURRENCY-DEFLATE \"1.00\") => 100
  29. (CURRENCY-DEFLATE \"1.0d0\") => 100
  30. ;; Ten Dollars / $10.00 / 1000 Cents / These are all the same thing
  31. (CURRENCY-DEFLATE 10) => 1000
  32. (CURRENCY-DEFLATE 10.00) => 1000
  33. (CURRENCY-DEFLATE 10.0d0) => 1000
  34. (CURRENCY-DEFLATE \"10\") => 1000
  35. (CURRENCY-DEFLATE \"10.00\") => 1000
  36. (CURRENCY-DEFLATE \"10.0d0\") => 1000")
  37. (:method ((number number))
  38. ;; CEILING or FLOOR?
  39. (ceiling (* 100 number)))
  40. (:method ((string string))
  41. (currency-deflate
  42. (read-from-string
  43. (if (eq (char string 0) #\$)
  44. (subseq string 1)
  45. string)))))
  46. (defgeneric currency-inflate (obj)
  47. (:documentation "CURRENCY-INFLATE takes a NUMBER and converts it into a STRING representation of the fixed point amount of cents (100 cents per dollar) into \"$DD.CC\" format.")
  48. (:method ((number number))
  49. (format nil "~$" (float (/ number 100)))))