clojurewerkz / money

A Clojure library that deals with monetary values and currencies. Built on top of Joda Money.
155 stars 20 forks source link

Support money output formatting #4

Closed glenjamin closed 11 years ago

glenjamin commented 11 years ago

Apologies if this already in somewhere, but I can't seem to find it in the API.

Joda Money provides http://joda-money.sourceforge.net/apidocs/org/joda/money/format/MoneyFormatter.html for outputting money nicely, but this is only available by reaching through to Joda from clojure at the moment.

Also, it would be nice to implement the hiccup HtmlRenderer protocol, but I suspect that is out of scope for this project? https://github.com/weavejester/hiccup/blob/master/src/hiccup/compiler.clj#L57

michaelklishin commented 11 years ago

Yes, formatting is not currently supported. Hiccup formatter can be added as an optional feature, similar to how Monger and Welle have core.cache implementations and cheshire integration.

glenjamin commented 11 years ago

For reference, here's what I threw into my project for now:

(extend-protocol HtmlRenderer
  Money
  (render-html [money]
    (-> (MoneyFormatterBuilder.)
        (.appendCurrencySymbolLocalized)
        (.appendAmountLocalized)
        (.toFormatter (Locale/UK))
        (.print money))))
michaelklishin commented 11 years ago

@glenjamin please try clojurewerkz.money.format/format (which will use the same formatter as you provided, and optionally takes locale as the 2nd argument) in 1.3.0-beta2. Hiccup integration is available in clojurewerkz.money.hiccup. I need to update the docs but there are tests and a couple of examples in the change log. Hiccup integration only requires compiling the namespace.

glenjamin commented 11 years ago

Cheers, that works nicely.

I had a bit of trouble sorting out the java default locale via system properties but I've got it sorted now, not sure if its worth trying to make that easier via this api though.

michaelklishin commented 11 years ago

1.3.0 is out