Name: money
Owner: ClojureWerkz
Description: A Clojure library that deals with monetary values and currencies. Built on top of Joda Money.
Created: 2012-03-11 14:44:30.0
Updated: 2017-12-21 19:28:47.0
Pushed: 2017-12-11 17:12:43.0
Size: 146
Language: Clojure
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
ClojureWerkz Money is a Clojure library that deals with monetary amounts. It is built on top of Joda Money.
Money is past 1.0
and is considered to be a complete, stable library.
Money artifacts are released to Clojars. If you are using Maven, add the following repository
definition to your pom.xml
:
ository>
d>clojars.org</id>
rl>http://clojars.org/repo</url>
pository>
With Leiningen:
[clojurewerkz/money "1.10.0"]
With Maven:
<dependency>
<groupId>clojurewerkz</groupId>
<artifactId>money</artifactId>
<version>1.10.0</version>
</dependency>
Monetary amounts are instantiated using clojurewerkz.money.amounts
functions. They operate on
floating point amounts (doubles) or long values in major units (e.g. dollars) or minor units (e.g. cents).
Note that some currencies do not have minor units (most notably JPY
). For those, use clojurewerkz.money.amounts/of-major
.
uire '[clojurewerkz.money.amounts :as ma])
uire '[clojurewerkz.money.currencies :as mc])
SD 10.50
amount-of mc/USD 10.5)
SD 10
of-major mc/USD 10)
SD 10.50
of-minor mc/USD 1050)
PY 1000
of-major mc/JPY 1000)
Note that not all currencies have minor units (most notably JPY does not).
It is possible to parse a string in the standard format [currency unit] [amount]
, e.g. JPY 1000
:
uire '[clojurewerkz.money.amounts :as ma])
parse "JPY 1000")
rg.joda.money.Money instance for JPY 1000
Monetary amounts can be added, substracted and so on using clojurewerkz.money.amounts/plus
,
clojurewerkz.money.amounts/minus
, clojurewerkz.money.amounts/multiply
, and
clojurewerkz.money.amounts/divide
functions:
uire '[clojurewerkz.money.amounts :as ma])
uire '[clojurewerkz.money.currencies :as mc])
plus (ma/amount-of mc/USD 10) (ma/amount-of mc/USD 100))
SD 110
minus (ma/amount-of mc/USD 100) (ma/amount-of mc/USD 10))
SD 90
multiply (ma/amount-of mc/USD 100) 10)
SD 1000
floor for flooring round mode
divide (ma/amount-of mc/USD 100.1) 10 :floor)
SD 10
It is possible to add up all monies in a collection or sequence using clojurewerkz.money.amounts/total
:
uire '[clojurewerkz.money.amounts :as ma])
uire '[clojurewerkz.money.currencies :as mc])
total [(ma/amount-of mc/USD 10) (ma/amount-of mc/USD 100)])
SD 110
It is possible to compare monetary amounts using >, >=, < and <=.
uire '[clojurewerkz.money.amounts :as ma])
uire '[clojurewerkz.money.currencies :as mc])
< (ma/amount-of mc/USD 100) (ma/amount-of mc/USD 100))
alse
<= (ma/amount-of mc/USD 100) (ma/amount-of mc/USD 100) (ma/amount-of mc/USD 120))
rue
>= (ma/amount-of mc/USD 100) (ma/amount-of mc/USD 100) (ma/amount-of mc/USD 120))
alse
> (ma/amount-of mc/USD 200) (ma/amount-of mc/USD 100))
rue
clojurewerkz.money.amounts/round
is a function that performs rounding of
monetary values using one of the rounding modes:
uire '[clojurewerkz.money.amounts :as ams])
/round (ams/amount-of cu/USD 40.01) -1 :floor)
SD 40
/round (ams/amount-of cu/USD 40.01) -1 :up)
SD 50
/round (ams/amount-of cu/USD 45.24) 0 :floor)
SD 45
/round (ams/amount-of cu/USD 45.24) 0 :up)
SD 46
/round (ams/amount-of cu/USD 45.24) 1 :floor)
SD 45.20
/round (ams/amount-of cu/USD 45.24) 1 :up)
SD 45.30
Currency units use their ISO-4217 codes and represented by org.joda.money.CurrencyUnit
instances.
Usually the easiest way to use currency units is via clojurewerkz.money.currencies
aliases:
uire '[clojurewerkz.money.currencies :as mc])
SD ;= USD currency unit
AD ;= CAD currency unit
BP ;= GBP currency unit
UB ;= RUB currency unit
clojurewerkz.money.currencies/for-code
and clojurewerkz.money.currencies/of-country
can be used
to get currency units by their ISO-4217 code strings and country abbreviations:
uire '[clojurewerkz.money.currencies :as mc])
for-code "CHF") ;= CHF currency unit
for-country "CH") ;= CHF currency unit
clojurewerkz.money.currencies/pseudo-currency?
is a predicate function that takes a currency unit
and returns true if it is a pseudo-currency (e.g. Bitcoin or IMF Special Drawing Rights).
clojurewerkz.money.amounts/convert-to
converts a monetary value in one currency
to another using provided exchange rate and rounding mode:
uire '[clojurewerkz.money.amounts :as ams])
/convert-to (ams/amount-of cu/GBP 65.65) cu/USD 1.523 :down)
SD 99.98
Money supports formatting of monetary amounts with the clojurewerkz.money.format/format
function
which takes an amount and (optionally) a locale and a formatter:
uire '[clojurewerkz.money.currencies :as cu])
uire '[clojurewerkz.money.amounts :refer [amount-of]])
uire '[clojurewerkz.money.format :refer :all])
ort java.util.Locale)
ormat using default system locale
mat (amount-of cu/GBP 20.0)) ;= GBP20,00
ormat using UK locale
mat (amount-of cu/GBP 20.0) Locale/UK) ;= £20.00
ormat using Brazilian locale
mat (amount-of cu/BRL 20.0) (Locale. "pt" "BR")) ;= R$20,00
Default formatter uses localized currency symbol and amount and default locale which JVM infers from environment settings.
clojurewerkz.money.json
, when loaded, registers serializers for
org.joda.money.Money
and org.joda.money.CurrencyUnit
with
Cheshire. Serialization conventions used are straightforward and
produce human readable values:
(clojurewerkz.money.currencies/USD)
=> "USD"
(clojurewerkz.money.amounts/amount-of (clojurewerkz.money.currencies/USD) 20.5)
=> "USD20.50"
(will use system locale by default)To use it, simply require the namespace and then use Cheshire generation functions as usual.
This extension requires Cheshire 5.0.x
or later. clojure.data.json
is not supported.
clojurewerkz.money.monger
, when loaded, registers BSON serializers
for org.joda.money.Money
and
org.joda.money.CurrencyUnit
. Serialization conventions used are
straightforward and produce human readable values:
(clojurewerkz.money.currencies/USD)
=> "USD"
(clojurewerkz.money.amounts/amount-of (clojurewerkz.money.currencies/USD) 20.5)
=> {"currency-unit" "USD" "amount-in-minor-units" 2050}
Note that serialization is one-way: loaded documents are returned as
maps because there is no way to tell them from regular BSON
documents. clojurewerkz.money.monger/from-stored-map
can be used to
produce Money
instances from maps following the serialization
convention described above.
clojurewerkz.money.hiccup
, when loaded, extends Hiccup HTML rendering protocol to render
monetary amounts and currency units.
Rendering conventions used are straightforward and
produce human readable values:
(clojurewerkz.money.currencies/USD)
=> "USD"
(clojurewerkz.money.amounts/amount-of (clojurewerkz.money.currencies/USD) 20.5)
=> "USD20.50"
(will use system locale by default)To use it, simply require the namespace and then use Hiccup as usual.
ClojureWerkz Money has a mailing list. Feel free to join it and ask any questions you may have.
To subscribe for announcements of releases, important changes and so on, please follow @ClojureWerkz on Twitter.
ClojureWerkz Money is built from the ground up for Clojure 1.4 and up. The most recent release is always recommended.
CI is hosted by travis-ci.org
Money uses Leiningen 2. Make sure you have it installed and then run tests against all supported Clojure versions using
lein all test
Then create a branch and make your changes on it. Once you are done with your changes and all tests pass, submit a pull request on GitHub.
Copyright © 2012-2016 Michael S. Klishin, Alex Petrov, and the ClojureWerkz team.
Double licensed under the Eclipse Public License (the same as Clojure) or the Apache Public License 2.0.