I'm using the following:
(defn dollars [input] (str "$" (format "%.2f" input)))
(which doesn't do the commas)
But I think there must be a method using pprint/cl-format
.
My question is: How to idiomatically print currency in Clojure?
If you have more to work with money, than just formatting it, you might consider using https://github.com/clojurewerkz/money (see the section Formatting), which wrapps joda-money
. This not only covers formatting, but also other common problems like rounding.
user=> (mf/format (ma/amount-of mc/USD 10000))
"$10,000.00"
user=> (mf/format (ma/amount-of mc/USD 10000) java.util.Locale/GERMANY)
"USD10.000,00"
edit
You can pass the amount-of
means of rounding. E.g.
user=> (mf/format (ma/amount-of mc/USD 10.111111111111111))
ArithmeticException Scale of amount 10.11111111111111 is greater than the scale of the currency USD org.joda.money.Money.of (Money.java:74)
user=> (mf/format (ma/amount-of mc/USD 10.111111111111111 (java.math.RoundingMode/HALF_DOWN)))
"$10.11"
See also Bankster: https://github.com/randomseed-io/bankster
(money/of :EUR 25)
; => #money[25.00 EUR]
(money/of 25 :EUR)
; => #money[25.00 EUR]
(money/of crypto/BTC 10.1)
; => #money/crypto[10.10000000 BTC]
(money/of BTC 10.1)
; => #money/crypto[10.10000000 BTC]
#money EUR
; => #money[0.00 EUR]
#money/crypto ETH
; => #money/crypto[0.000000000000000000 ETH]
#money[PLN 2.50]
; => #money[2.50 PLN]
#currency USD
; => #currency{:id :USD, :domain :ISO-4217, :kind :FIAT, :numeric 840, :scale 2}
(currency/symbol :USD)
; => "USD"
(currency/symbol :USD :en_US)
; => "$"
(currency/symbol :USDT)
; => "₮"
(currency/name :USDT)
; => "Tether"
(money/format #money[10 EUR])
; => "10,00 €"
(money/format #money[10 EUR] :en_US)
; => "€10.00"
(money/format #money[10 EUR] :pl {:currency-symbol-fn currency/name})
; => "10,00 euro"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With