????MonetaryAmount?????
??????????MonetaryAmount?????????????
????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: javax.money.MonetaryException: Currency mismatch: EUR/USD
????fiveEuro.add(tenUsDollar);
??????????????????????????????????MonetaryAmount??????????????????????????????
????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
????????12.3456????????????????????????????л???
?????????MonetaryAmount?????????????????????????????????й??????????????顣??Щ????????????Java 8????API?????????á?
?????????????????????
????List<MonetaryAmount> 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"));
??????????????CurrencyUnit?????н??????
????CurrencyUnit yen = MonetaryCurrencies.getCurrency("JPY");
????CurrencyUnit dollar = MonetaryCurrencies.getCurrency("USD");
????// ????????????????????
????// result is [USD 18?? USD 7?? USD 42]
????List<MonetaryAmount> onlyDollar = amounts.stream()
????.filter(MonetaryFunctions.isCurrency(dollar))
????.collect(Collectors.toList());
????// ????????????????????????
????// [USD 18?? USD 7?? JPY 13.37?? USD 42]
????List<MonetaryAmount> onlyDollarAndYen = amounts.stream()
????.filter(MonetaryFunctions.isCurrency(dollar?? yen))
????.collect(Collectors.toList());
??????????????????????С????????????
????MonetaryAmount tenDollar = Money.of(10?? dollar);
????// [USD 42?? USD 18]
????List<MonetaryAmount> greaterThanTenDollar = amounts.stream()
????.filter(MonetaryFunctions.isCurrency(dollar))
????.filter(MonetaryFunctions.isGreaterThan(tenDollar))
????.collect(Collectors.toList());
?????????????????
????// Sorting dollar values by number value
????// [USD 7?? USD 18?? USD 42]
????List<MonetaryAmount> sortedByAmount = onlyDollar.stream()
????.sorted(MonetaryFunctions.sortNumber())
????.collect(Collectors.toList());
????// Sorting by CurrencyUnit
????// [EUR 2?? JPY 13.37?? USD 42?? USD 7?? USD 18]
????List<MonetaryAmount> sortedByCurrencyUnit = amounts.stream()
????.sorted(MonetaryFunctions.sortCurrencyUnit())
????.collect(Collectors.toList());
???????з????????
????// ???????λ???з???
????// {USD=[USD 42?? USD 7?? USD 18]?? EUR=[EUR 2]?? JPY=[JPY 13.37]}
????Map<CurrencyUnit?? List<MonetaryAmount>> groupedByCurrency = amounts.stream()
????.collect(MonetaryFunctions.groupByCurrencyUnit());
????// ???鰱???л???
????Map<CurrencyUnit?? MonetarySummaryStatistics> summary = amounts.stream()
????.collect(MonetaryFunctions.groupBySummarizingMonetary()).get();
????// 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??????????????????????????????С??????????
????List<MonetaryAmount> amounts = new ArrayList<>();
????amounts.add(Money.of(10?? "EUR"));
????amounts.add(Money.of(7.5?? "EUR"));
????amounts.add(Money.of(12?? "EUR"));
????Optional<MonetaryAmount> max = amounts.stream().reduce(MonetaryFunctions.max()); // "EUR 7.5"
????Optional<MonetaryAmount> min = amounts.stream().reduce(MonetaryFunctions.min()); // "EUR 12"
????Optional<MonetaryAmount> sum = amounts.stream().reduce(MonetaryFunctions.sum()); // "EUR 29.5"
??????????MonetaryAmount????
????MonetaryAmount????????????????????????MonetaryOperator??MonetaryOperator???????????????????????MonetaryAmount??β?????????μ?MonetaryAmount????
????// 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
?????????API??????????MonetaryOperator???????????????????濴???????????????MonetaryOperator??????????????
????????
??????????????????ExchangeRateProvider???????JavaMoney????????????ExchangeRateProvider?????????????????????ECBCurrentRateProvider?? IMFRateProvider??
????ECBCurrentRateProvider??????????????????(European Central Bank??ECB)???????IMFRateProvider?????????????????????International Monetary Fund??IMF????????
????// get the default ExchangeRateProvider (CompoundRateProvider)
????ExchangeRateProvider exchangeRateProvider = MonetaryConversions.getExchangeRateProvider();
????// get the names of the default provider chain
????// [IDENT?? ECB?? IMF?? ECB-HIST]
????List<String> defaultProviderChain = MonetaryConversions.getDefaultProviderChain();
????// get a specific ExchangeRateProvider (here ECB)
????ExchangeRateProvider ecbExchangeRateProvider = MonetaryConversions.getExchangeRateProvider("ECB");