Java 9 new currency API

By Mary Johnson,2015-07-21 08:55
26 views 0
Java 9 new currency API

    Java 9 new currency API

    / get MonetaryAmount from CurrencyUnit

    CurrencyUnit euro = MonetaryCurrencies.getCurrency("EUR");

    MonetaryAmount fiveEuro = Money.of(5, euro);

    // get MonetaryAmount from currency code

    MonetaryAmount tenUsDollar = Money.of(10, "USD");

    // FastMoney is an alternative MonetaryAmount factory that focuses on performance MonetaryAmount sevenEuro = FastMoney.of(7, euro);

    Money is JavaMoney repository with FastMoney MonetaryAmount two

    implementations.Money is the default implementation, it use BigDecimal to storage amount.FastMoney another implementation is optional, it with long storage amount.According to the document, FastMoney operations on about 10 to 15 times faster than Money.However, FastMoney size and accuracy are limited by the amount of long type.

    Note, here is the Money and FastMoney are specific implementation class (in org. Javamoney. Moneta. * package below, rather than javax.mail. Money. *).If you don't want to specify the specific type, can be generated through MonetaryAmountFactory a MonetaryAmount instances:

    MonetaryAmount specAmount = MonetaryAmounts.getDefaultAmountFactory()

     .setNumber(123.45) .setCurrency("USD") .create();

    If and only if the implementation class, monetary unit, and the value is equal to all that the two MontetaryAmount instances are equal.

    MonetaryAmount oneEuro = Money.of(1, MonetaryCurrencies.getCurrency("EUR")); boolean isEqual = oneEuro.equals(Money.of(1, "EUR")); // true

    boolean isEqualFast = oneEuro.equals(FastMoney.of(1, "EUR")); // false

    MonetaryAmount method contains rich, can be used to retrieve specific currency, amount, precision, etc. :

    MonetaryAmount monetaryAmount = Money.of(123.45, euro);

    CurrencyUnit currency = monetaryAmount.getCurrency();

NumberValue numberValue = monetaryAmount.getNumber();

    int intValue = numberValue.intValue(); // 123

    double doubleValue = numberValue.doubleValue(); // 123.45

    long fractionDenominator = numberValue.getAmountFractionDenominator(); // 100 long fractionNumerator = numberValue.getAmountFractionNumerator(); // 45 int precision = numberValue.getPrecision(); // 5

    // NumberValue extends java.lang.Number.

    // So we assign numberValue to a variable of type Number

    Number number = numberValue;

    The use of MonetaryAmount

    Can be conducted on MonetaryAmount arithmetic:

    MonetaryAmount twelveEuro = fiveEuro.add(sevenEuro); // "EUR 12" MonetaryAmount twoEuro = sevenEuro.subtract(fiveEuro); // "EUR 2" MonetaryAmount sevenPointFiveEuro = fiveEuro.multiply(1.5); // "EUR 7.5" // MonetaryAmount can have a negative NumberValue

    MonetaryAmount minusTwoEuro = fiveEuro.subtract(sevenEuro); // "EUR -2" // some useful utility methods

    boolean greaterThan = sevenEuro.isGreaterThan(fiveEuro); // true boolean positive = sevenEuro.isPositive(); // true

    boolean zero = sevenEuro.isZero(); // false

    // Note that MonetaryAmounts need to have the same CurrencyUnit to do mathematical operations

    // this fails with: Currency mismatch: EUR/USD fiveEuro.add(tenUsDollar);

    Rounding operation is part of the amount of conversion inside is very important.MonetaryAmount can use rounding rounded operator to:

    CurrencyUnit usd = MonetaryCurrencies.getCurrency("USD"); MonetaryAmount dollars = Money.of(12.34567, usd);

    MonetaryOperator roundingOperator = MonetaryRoundings.getRounding(usd); MonetaryAmount roundedDollars = dollars.with(roundingOperator); // USD 12.35

    There will be $12.3456 according to the current monetary default rounding rules for


    The operating MonetaryAmount collection, there are many practical tool method can be

    used for filtering, sorting, and grouping.These methods can also be used with Java 8

    streaming API.

    Take a look at the following this collection:

    List amounts = new ArrayList<>();

    amounts.add(Money.of(2, "EUR"));

    amounts.add(Money.of(42, "USD"));

    amounts.add(Money.of(7, "USD"));

    amounts.add(Money.of(13.37, "JPY"));

    amounts.add(Money.of(18, "USD"));

    We can according to the CurrencyUnit amounts filtering: CurrencyUnit yen = MonetaryCurrencies.getCurrency("JPY"); CurrencyUnit dollar = MonetaryCurrencies.getCurrency("USD"); // 根据货币过滤?只返回美金

    // result is [USD 18, USD 7, USD 42]

    List onlyDollar =



    // 根据货币过滤?只返回美金和日元

    // [USD 18, USD 7, JPY 13.37, USD 42]

    List onlyDollarAndYen =

     .filter(MonetaryFunctions.isCurrency(dollar, yen))


    We can also filter out is greater than or less than a certain threshold amount:

    MonetaryAmount tenDollar = Money.of(10, dollar); // [USD 42, USD 18]

    List greaterThanTenDollar =




    Sorting is similar:

    // Sorting dollar values by number value

    // [USD 7, USD 18, USD 42]

    List sortedByAmount =



    // Sorting by CurrencyUnit

    // [EUR 2, JPY 13.37, USD 42, USD 7, USD 18]

    List sortedByCurrencyUnit =



    And grouping operation:

    // 按货币单位进行分组

    // {USD=[USD 42, USD 7, USD 18], EUR=[EUR 2], JPY=[JPY 13.37]} Map> groupedByCurrency =


    // 分组并进行汇总

    Map summary =


// get summary for CurrencyUnit USD

    MonetarySummaryStatistics dollarSummary = summary.get(dollar);

    MonetaryAmount average = dollarSummary.getAverage(); // "USD 22.333333333333333333.." MonetaryAmount min = dollarSummary.getMin(); // "USD 7"

    MonetaryAmount max = dollarSummary.getMax(); // "USD 42"

    MonetaryAmount sum = dollarSummary.getSum(); // "USD 67"

    long count = dollarSummary.getCount(); // 3

    MonetaryFunctions also provides reduction function can be used to obtain the maximum, minimum, and the sum:

    List amounts = new ArrayList<>();

    amounts.add(Money.of(10, "EUR"));

    amounts.add(Money.of(7.5, "EUR"));

    amounts.add(Money.of(12, "EUR"));

    Optional max =; // "EUR 7.5"

    Optional min =; // "EUR 12"

    Optional sum =; //

    Custom MonetaryAmount operation

    MonetaryAmount also provides a very friendly extension point is called MonetaryOperator.MonetaryOperator is a functional interface, it receives a MonetaryAmount into arguments and returns a new MonetaryAmount object.

    // A monetary operator that returns 10% of the input MonetaryAmount

    // Implemented using Java 8 Lambdas

    MonetaryOperator tenPercentOperator = (MonetaryAmount amount) -> {

     BigDecimal baseAmount = amount.getNumber().numberValue(BigDecimal.class);

     BigDecimal tenPercent = baseAmount.multiply(new BigDecimal("0.1"));

     return Money.of(tenPercent, amount.getCurrency());


    MonetaryAmount dollars = Money.of(12.34567, "USD");

    // apply tenPercentOperator to MonetaryAmount

    MonetaryAmount tenPercentDollars = dollars.with(tenPercentOperator); // USD 1.234567

    Standard API is achieved through MonetaryOperator interface features.See in front of the rounding operation, for example, is in the form of MonetaryOperator interface.

    The exchange rate

    Could be obtained by ExchangeRateProvider currency exchange rate.JavaMoney comes with a number of different ExchangeRateProvider implementation.One of the most important two are ECBCurrentRateProvider and IMFRateProvider.

    ECBCurrentRateProvider query is the European Central Bank (European Central Bank, the ECB) data and IMFRateProvider query is the International Monetary Fund (International Monetary Fund, the IMF) exchange rate.

    // get the default ExchangeRateProvider (CompoundRateProvider)

    ExchangeRateProvider exchangeRateProvider =


    // get the names of the default provider chain


    List defaultProviderChain = MonetaryConversions.getDefaultProviderChain();

// get a specific ExchangeRateProvider (here ECB)

    ExchangeRateProvider ecbExchangeRateProvider =


    If you do not specify a return is CompoundRateProvider ExchangeRateProvider words.CompoundRateProvider exchange rate conversion requests will be delegated to a ExchangeRateProvider chain and will return to accurate results of the first provider's data back.

    // get the exchange rate from euro to us dollar

    ExchangeRate rate = exchangeRateProvider.getExchangeRate("EUR", "USD"); NumberValue factor = rate.getFactor(); // 1.2537 (at time writing)

    CurrencyUnit baseCurrency = rate.getBaseCurrency(); // EUR

    CurrencyUnit targetCurrency = rate.getCurrency(); // USD

    Currency conversion

    The transformation between the different currencies can be done by CurrencyConversions ExchangeRateProvider returns.

    // get the CurrencyConversion from the default provider chain

    CurrencyConversion dollarConversion = MonetaryConversions.getConversion("USD"); // get the CurrencyConversion from a specific provider

    CurrencyConversion ecbDollarConversion =


    MonetaryAmount tenEuro = Money.of(10, "EUR");

    // convert 10 euro to us dollar

    MonetaryAmount inDollar = tenEuro.with(dollarConversion); // "USD 12.537" (at the time writing)

    Please note that CurrencyConversion also realized MonetaryOperator interface.Just like other operating, it can also through MonetaryAmount. With () method to call.

    Formatting and parsing

    MonetaryAmount MonetaryAmountFormat to with string parsing/format.

// formatting by locale specific formats

    MonetaryAmountFormat germanFormat = MonetaryFormats.getAmountFormat(Locale.GERMANY); MonetaryAmountFormat usFormat = MonetaryFormats.getAmountFormat(Locale.CANADA); MonetaryAmount amount = Money.of(12345.67, "USD");

    String usFormatted = usFormat.format(amount); // "USD12,345.67"

    String germanFormatted = germanFormat.format(amount); // 12.345,67 USD // A MonetaryAmountFormat can also be used to parse MonetaryAmounts from strings MonetaryAmount parsed = germanFormat.parse("12,4 USD");

    Can be generated through AmountFormatQueryBuilder custom format. // Creating a custom MonetaryAmountFormat

    MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat(



     .set("pattern", "00,00,00,00.00 ?")


    // results in "00,01,23,45.67 US Dollar"

    String formatted = customFormat.format(amount);

    Note the problem of the symbols in the pattern string is a placeholder as currency

Report this document

For any questions or suggestions please email