当前位置: 首页>>代码示例>>Java>>正文


Java Monetary类代码示例

本文整理汇总了Java中javax.money.Monetary的典型用法代码示例。如果您正苦于以下问题:Java Monetary类的具体用法?Java Monetary怎么用?Java Monetary使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


Monetary类属于javax.money包,在下文中一共展示了Monetary类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testReciprocal

import javax.money.Monetary; //导入依赖的package包/类
/**
 * Test method for
 * {@link org.javamoney.moneta.function.MonetaryOperators#reciprocal()}.
 */
@Test
public void testReciprocal() {
	MonetaryAmount m = Monetary.getDefaultAmountFactory()
			.setCurrency("CHF").setNumber(200).create();
	MonetaryAmount r = m.with(MonetaryOperators.reciprocal());
       //noinspection BigDecimalMethodWithoutRoundingCalled
       assertEquals(
			Monetary.getDefaultAmountFactory().setCurrency("CHF")
					.setNumber(BigDecimal.ONE.divide(BigDecimal.valueOf(200)))
					.create(),
			r);
}
 
开发者ID:JavaMoney,项目名称:jsr354-ri-bp,代码行数:17,代码来源:MonetaryOperatorsTest.java

示例2: apply

import javax.money.Monetary; //导入依赖的package包/类
@Override
public MonetaryAmount apply(MonetaryAmount amount) {
    MonetaryOperator minorRounding = Monetary
            .getRounding(RoundingQueryBuilder.of().set("scale", 2).set(RoundingMode.HALF_UP).build());
    MonetaryAmount amt = amount.with(minorRounding);
    MonetaryAmount mp = amt.with(MonetaryOperators.minorPart());
    if (mp.isGreaterThanOrEqualTo(
            Monetary.getDefaultAmountFactory().setCurrency(amount.getCurrency()).setNumber(0.03)
                    .create())) {
        // add
        return amt.add(Monetary.getDefaultAmountFactory().setCurrency(amt.getCurrency())
                .setNumber(new BigDecimal("0.05")).create().subtract(mp));
    } else {
        // subtract
        return amt.subtract(mp);
    }
}
 
开发者ID:JavaMoney,项目名称:jsr354-ri-bp,代码行数:18,代码来源:TestRoundingProvider.java

示例3: setup

import javax.money.Monetary; //导入依赖的package包/类
@BeforeMethod
public void setup() {
  MockitoAnnotations.initMocks(this);

  // Enable debug mode...
  ((ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME))
      .setLevel(Level.DEBUG);

  ledgerInfo = ImmutableLedgerInfo.builder()
      .currencyScale(8)
      .currencyUnit(Monetary.getCurrency("USD"))
      .ledgerPrefix(LEDGER_PREFIX)
      .build();
  final SimulatedLedger simulatedLedger = new SimulatedLedger(ledgerInfo);

  // Initialize the ledger plugin under test...
  this.mockLedgerPlugin = new MockLedgerPlugin(getLedgerPluginConfig(), simulatedLedger);
  mockLedgerPlugin.addLedgerPluginEventHandler(ledgerPluginEventHandlerMock);

  mockLedgerPlugin.connect();

  // Reset the event handler so we don't count the "connect" event, in general
  reset(ledgerPluginEventHandlerMock);
}
 
开发者ID:interledger,项目名称:java-ilp-plugin,代码行数:25,代码来源:MockLedgerPluginTest.java

示例4: main

import javax.money.Monetary; //导入依赖的package包/类
public static void main(String[] args) {
    CurrencyUnit dollar = Monetary.getCurrency("USD");
    CurrencyUnit real = Monetary.getCurrency("BRL");



    MonetaryAmount money = Money.of(10, dollar);
    MonetaryAmount money2 = Money.of(10, real);
    MonetaryAmount money3 = Money.of(10, dollar);
    MonetaryAmount money4 = Money.of(9, real);
    MonetaryAmount money5 = Money.of(25, dollar);

    GroupMonetarySummaryStatistics grouped = Stream.of(money, money2, money3, money4, money5)
    .collect(MonetaryFunctions.groupBySummarizingMonetary());
    Map<CurrencyUnit, MonetarySummaryStatistics> mapSummary = grouped.get();
    MonetarySummaryStatistics summary = mapSummary.get(dollar);

    MonetaryAmount min = summary.getMin();//USD 10
    MonetaryAmount max = summary.getMax();//USD 25
    MonetaryAmount average = summary.getAverage();//USD 15
    long count = summary.getCount();//3
    MonetaryAmount sum = summary.getSum();//USD 45

}
 
开发者ID:otaviojava,项目名称:money-api-book-samples,代码行数:25,代码来源:AggregateGroupSummaringMonetaryAmount.java

示例5: testNumberInsignificanceForRates

import javax.money.Monetary; //导入依赖的package包/类
@Test
public void testNumberInsignificanceForRates(){
	ExchangeRate rateFromString = new ExchangeRateBuilder(ConversionContext.HISTORIC_CONVERSION)
			.setBase(Monetary.getCurrency("USD"))
			.setTerm(Monetary.getCurrency("EUR"))
			.setFactor(DefaultNumberValue.of(new BigDecimal("1.1")))
			.build();

	ExchangeRate rateFromDouble = new ExchangeRateBuilder(ConversionContext.HISTORIC_CONVERSION)
			.setBase(Monetary.getCurrency("USD"))
			.setTerm(Monetary.getCurrency("EUR"))
			.setFactor(DefaultNumberValue.of(1.1))
			.build();

	assertEquals(rateFromDouble, rateFromString, "Rates are not equal for same factor.");
}
 
开发者ID:JavaMoney,项目名称:jsr354-ri-bp,代码行数:17,代码来源:ExchangeRateBuilderTest.java

示例6: main

import javax.money.Monetary; //导入依赖的package包/类
public static void main(String[] args) {
    CurrencyUnit dollar = Monetary.getCurrency("USD");
    CurrencyUnit euro = Monetary.getCurrency("EUR");
    CurrencyUnit real = Monetary.getCurrency("BRL");

    MonetaryAmount money = Money.of(9, euro);
    MonetaryAmount money2 = Money.of(10, dollar);
    MonetaryAmount money3 = Money.of(11, real);

    ExchangeRateProvider provider =
            MonetaryConversions.getExchangeRateProvider(ExchangeRateType.IMF);

    ;
    List<MonetaryAmount> resultAsc = Stream.of(money, money2, money3)
            .sorted(MonetaryFunctions
                    .sortValuable(provider))
            .collect(Collectors.toList());//[BRL 11, EUR 9, USD 10]

    List<MonetaryAmount> resultDesc = Stream.of(money, money2, money3)
            .sorted(MonetaryFunctions
                    .sortValuableDesc(provider)).collect(Collectors.toList());//[USD 10, EUR 9, BRL 11]

}
 
开发者ID:otaviojava,项目名称:money-api-book-samples,代码行数:24,代码来源:SortMonetaryAmountExchange.java

示例7: main

import javax.money.Monetary; //导入依赖的package包/类
public static void main(String[] args) {
    CurrencyUnit dollar = Monetary.getCurrency("USD");
    CurrencyUnit euro = Monetary.getCurrency("EUR");
    CurrencyUnit real = Monetary.getCurrency("BRL");

    MonetaryAmount money = Money.of(9, euro);
    MonetaryAmount money2 = Money.of(10, dollar);
    MonetaryAmount money3 = Money.of(11, real);

    List<MonetaryAmount> resultAsc = Stream.of(money, money2, money3)
            .sorted(MonetaryFunctions
                    .sortNumber()).collect(Collectors.toList());//[EUR 9, USD 10, BRL 11]
    List<MonetaryAmount> resultDesc = Stream.of(money, money2, money3)
            .sorted(MonetaryFunctions
                    .sortNumberDesc()).collect(Collectors.toList());//[BRL 11, USD 10, EUR 9]
}
 
开发者ID:otaviojava,项目名称:money-api-book-samples,代码行数:17,代码来源:SortMonetaryAmountNumber.java

示例8: main

import javax.money.Monetary; //导入依赖的package包/类
public static void main(String[] args) {
    CurrencyUnit dollar = Monetary.getCurrency("USD");



    MonetaryAmount money = Money.of(10, dollar);
    MonetaryAmount money2 = Money.of(10, dollar);
    MonetaryAmount money3 = Money.of(10, dollar);
    MonetaryAmount money4 = Money.of(9, dollar);
    MonetaryAmount money5 = Money.of(8, dollar);

    List<MonetaryAmount> greaterThanZero = Stream.of(money, money2, money3, money4, money5)
            .filter(MonetaryFunctions.isGreaterThan(Money.zero(dollar)))
            .collect(Collectors.toList());//[USD 10, USD 10, USD 10, USD 9, USD 8]

    boolean hasAnyGreaterThanZero = Stream.of(money, money2, money3, money4, money5)
            .anyMatch(MonetaryFunctions.isGreaterThan(Money.zero(dollar)));//true

    boolean allBetweenZeroAndTen = Stream.of(money, money2, money3, money4, money5)
            .allMatch(MonetaryFunctions.isBetween(Money.zero(dollar),
                    Money.of(BigDecimal.TEN, dollar)));//true

}
 
开发者ID:otaviojava,项目名称:money-api-book-samples,代码行数:24,代码来源:PredicateMonetaryAmountNumberValue.java

示例9: main

import javax.money.Monetary; //导入依赖的package包/类
public static void main(String[] args) {
    CurrencyUnit dollar = Monetary.getCurrency("USD");
    CurrencyUnit euro = Monetary.getCurrency("EUR");
    ExchangeRateProvider provider = MonetaryConversions.getExchangeRateProvider(ExchangeRateType.IMF);

    MonetaryAmount money = Money.of(10, dollar);
    MonetaryAmount money2 = Money.of(10, euro);
    MonetaryAmount money3 = Money.of(10, dollar);
    MonetaryAmount money4 = Money.of(9, euro);
    MonetaryAmount money5 = Money.of(8, dollar);

    Optional<MonetaryAmount> max = Stream.of(money, money2, money3, money4, money5).reduce(
            MonetaryFunctions.max(provider));//javax.money.MonetaryException: Currency mismatch: BRL/USD
    max.ifPresent(System.out::println);//EUR 10

    Optional<MonetaryAmount> min = Stream.of(money, money2, money3, money4, money5).reduce(
            MonetaryFunctions.min(provider));
    min.ifPresent(System.out::println);//USD 8
    
}
 
开发者ID:otaviojava,项目名称:money-api-book-samples,代码行数:21,代码来源:ReduceMaxMinMonetaryAmountExchange.java

示例10: main

import javax.money.Monetary; //导入依赖的package包/类
public static void main(String[] args) {
    CurrencyUnit dollar = Monetary.getCurrency("USD");
    CurrencyUnit real = Monetary.getCurrency("BRL");

    ExchangeRateProvider provider = MonetaryConversions.getExchangeRateProvider(ExchangeRateType.IMF);


    MonetaryAmount money = Money.of(10, dollar);
    MonetaryAmount money2 = Money.of(10, real);
    MonetaryAmount money3 = Money.of(10, dollar);
    MonetaryAmount money4 = Money.of(9, real);
    MonetaryAmount money5 = Money.of(25, dollar);

    MonetarySummaryStatistics summary = Stream.of(money, money2, money3, money4, money5)
            .collect(ConversionOperators.summarizingMonetary(dollar, provider));

    MonetaryAmount min = summary.getMin();//USD 2.831248
    MonetaryAmount max = summary.getMax();//USD 25
    MonetaryAmount average = summary.getAverage();//USD 10.195416
    long count = summary.getCount();//5
    MonetaryAmount sum = summary.getSum();//50.97708

}
 
开发者ID:otaviojava,项目名称:money-api-book-samples,代码行数:24,代码来源:AggregateSummaringExchangeRateMonetaryAmount.java

示例11: testWithCurrencyUnitNumber

import javax.money.Monetary; //导入依赖的package包/类
/**
 * Test method for
 * {@link Money#getFactory()#setCurrency(javax.money.CurrencyUnit)} and {@link Money#getFactory()#setNumber
 * (Number)}.
 */
@Test
public void testWithCurrencyUnitNumber() {
    Money[] moneys = new Money[]{Money.of(100, "CHF"), Money.of(34242344, "USD"), Money.of(23123213.435, "EUR"),
            Money.of(-23123213.435, "USS"), Money.of(-23123213, "USN"), Money.of(0, "GBP")};
    Money s = Money.of(10, "XXX");
    MonetaryAmount[] moneys2 = new MonetaryAmount[]{
            s.getFactory().setCurrency(Monetary.getCurrency("CHF")).setNumber(100).create(),
            s.getFactory().setCurrency(Monetary.getCurrency("USD")).setNumber(34242344).create(),
            s.getFactory().setCurrency(Monetary.getCurrency("EUR"))
                    .setNumber(new BigDecimal("23123213.435")).create(),
            s.getFactory().setCurrency(Monetary.getCurrency("USS"))
                    .setNumber(new BigDecimal("-23123213.435")).create(),
            s.getFactory().setCurrency(Monetary.getCurrency("USN")).setNumber(-23123213).create(),
            s.getFactory().setCurrency(Monetary.getCurrency("GBP")).setNumber(0).create()};
    for (int i = 0; i < moneys.length; i++) {
        assertEquals(moneys[i], moneys2[i], "with(Number) failed.");
    }
}
 
开发者ID:JavaMoney,项目名称:jsr354-ri-bp,代码行数:24,代码来源:MoneyTest.java

示例12: main

import javax.money.Monetary; //导入依赖的package包/类
public static void main(String[] args) {
    CurrencyUnit dollar = Monetary.getCurrency("USD");


    MonetaryAmount money = Money.of(10, dollar);
    MonetaryAmount money2 = Money.of(10, dollar);
    MonetaryAmount money3 = Money.of(10, dollar);
    MonetaryAmount money4 = Money.of(9, dollar);
    MonetaryAmount money5 = Money.of(8, dollar);

    Optional<MonetaryAmount> max = Stream.of(money, money2, money3, money4, money5).reduce(
            MonetaryFunctions.max());//javax.money.MonetaryException: Currency mismatch: BRL/USD
    max.ifPresent(System.out::println);//USD 10

    Optional<MonetaryAmount> min = Stream.of(money, money2, money3, money4, money5).reduce(
            MonetaryFunctions.min());
    min.ifPresent(System.out::println);//USD 8
    
}
 
开发者ID:otaviojava,项目名称:money-api-book-samples,代码行数:20,代码来源:ReduceMaxMinMonetaryAmount.java

示例13: main

import javax.money.Monetary; //导入依赖的package包/类
public  static void main(String[] args) {
    CurrencyUnit currency = Monetary.getCurrency("BRL");
    MonetaryAmount money = FastMoney.of(10, currency);
    MonetaryOperator doubleOperator = m -> m.multiply(2);
    MonetaryAmount result = doubleOperator.apply(money);//BRL 20.00000
    MonetaryAmount result2 = result.with(doubleOperator);//BRL 40.00000
}
 
开发者ID:otaviojava,项目名称:money-api-book-samples,代码行数:8,代码来源:HelloMonetaryOperator.java

示例14: startElement

import javax.money.Monetary; //导入依赖的package包/类
@Override
public void startElement(String uri, String localName, String qName,
                         Attributes attributes) throws SAXException {
    if ("Cube".equals(qName)) {
        if (attributes.getValue("time")!=null) {
            // <Cube time="2015-03-13">...
            this.localDate = parseLocalDate(attributes.getValue("time"));
        }
        if (attributes.getValue("currency")!=null) {
            // read data <Cube currency="USD" rate="1.3349"/>
            CurrencyUnit tgtCurrency = Monetary
                    .getCurrency(attributes.getValue("currency"));
            addRate(tgtCurrency, this.localDate, BigDecimal.valueOf(Double
                    .parseDouble(attributes.getValue("rate"))));
        }
    }
    super.startElement(uri, localName, qName, attributes);
}
 
开发者ID:JavaMoney,项目名称:jsr354-ri-bp,代码行数:19,代码来源:ECBRateReader.java

示例15: shouldRoundedMonetaryOperatorWhenTheImplementationIsMoney

import javax.money.Monetary; //导入依赖的package包/类
@Test
public void shouldRoundedMonetaryOperatorWhenTheImplementationIsMoney() {
	int scale = 3;
	int precision = 5;

	CurrencyUnit real = Monetary.getCurrency("BRL");
	BigDecimal valueOf = BigDecimal.valueOf(35.34567);
	MonetaryAmount money = Money.of(valueOf, real);

	MathContext mathContext = new MathContext(precision, RoundingMode.HALF_EVEN);
	PrecisionScaleRoundedOperator monetaryOperator = PrecisionScaleRoundedOperator.of(scale, mathContext);

	MonetaryAmount result = monetaryOperator.apply(money);
	assertTrue(RoundedMoney.class.isInstance(result));
	assertEquals(result.getCurrency(), real);
	assertEquals(result.getNumber().getScale(), scale);
	assertEquals(result.getNumber().getPrecision(), precision);
	assertEquals(BigDecimal.valueOf(35.346), result.getNumber().numberValue(BigDecimal.class));



}
 
开发者ID:JavaMoney,项目名称:jsr354-ri-bp,代码行数:23,代码来源:PrecisionScaleRoundedOperatorTest.java


注:本文中的javax.money.Monetary类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。