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


Java MonetaryAmount.with方法代码示例

本文整理汇总了Java中javax.money.MonetaryAmount.with方法的典型用法代码示例。如果您正苦于以下问题:Java MonetaryAmount.with方法的具体用法?Java MonetaryAmount.with怎么用?Java MonetaryAmount.with使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.money.MonetaryAmount的用法示例。


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

示例1: testReciprocal

import javax.money.MonetaryAmount; //导入方法依赖的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.MonetaryAmount; //导入方法依赖的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: main

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

    MonetaryAmount money = Money.of(120.231, currency);

    MonetaryAmount majorPartResult = money.with(MonetaryOperators.majorPart());//BRL 120
    MonetaryAmount minorPartResult = money.with(MonetaryOperators.minorPart());//BRL 0.231
    MonetaryAmount percentResult = money.with(MonetaryOperators.percent(20));//BRL 24.0462
    MonetaryAmount permilResult = money.with(MonetaryOperators.permil(100));//BRL 12.0231
    MonetaryAmount roundingResult = money.with(MonetaryOperators.rounding());//BRL 120.23
    MonetaryAmount resultExchange = money.with(ConversionOperators.exchange(dollar));//USD 120.231
}
 
开发者ID:otaviojava,项目名称:money-api-book-samples,代码行数:14,代码来源:MonetaryOperatorsExample.java

示例4: main

import javax.money.MonetaryAmount; //导入方法依赖的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

示例5: testPermilBigDecimal

import javax.money.MonetaryAmount; //导入方法依赖的package包/类
/**
 * Test method for
 * {@link org.javamoney.moneta.function.MonetaryOperators#permil(java.math.BigDecimal)}
 * .
 */
@Test
public void testPermilBigDecimal() {
	MonetaryAmount m = Monetary.getDefaultAmountFactory()
			.setCurrency(
					"CHF").setNumber(100).create();
	MonetaryAmount r = m.with(MonetaryOperators.permil(BigDecimal
			.valueOf(25)));
	assertEquals(
			Monetary.getDefaultAmountFactory().setCurrency("CHF")
					.setNumber(
							new BigDecimal("2.5")).create(),
			r);
}
 
开发者ID:JavaMoney,项目名称:jsr354-ri-bp,代码行数:19,代码来源:MonetaryOperatorsTest.java

示例6: testPermilNumber

import javax.money.MonetaryAmount; //导入方法依赖的package包/类
/**
 * Test method for
 * {@link org.javamoney.moneta.function.MonetaryOperators#permil(java.lang.Number)}
 * .
 */
@Test
public void testPermilNumber() {
	MonetaryAmount m = Monetary.getDefaultAmountFactory()
			.setCurrency(
					"CHF").setNumber(100).create();
	MonetaryAmount r = m.with(MonetaryOperators.permil(25));
	assertEquals(
			Monetary.getDefaultAmountFactory().setCurrency("CHF")
					.setNumber(
							new BigDecimal("2.5")).create(),
			r);
}
 
开发者ID:JavaMoney,项目名称:jsr354-ri-bp,代码行数:18,代码来源:MonetaryOperatorsTest.java

示例7: testPermilNumberMathContext

import javax.money.MonetaryAmount; //导入方法依赖的package包/类
/**
 * Test method for
 * {@link org.javamoney.moneta.function.MonetaryOperators#permil(java.lang.Number, java.math.MathContext)}
 * .
 */
@Test
public void testPermilNumberMathContext() {
	MonetaryAmount m = Monetary.getDefaultAmountFactory()
			.setCurrency(
					"CHF").setNumber(100).create();
	MonetaryAmount r = m.with(MonetaryOperators.permil(25,
			MathContext.DECIMAL64));
	assertEquals(
			Monetary.getDefaultAmountFactory().setCurrency("CHF")
					.setNumber(
							new BigDecimal("2.5")).create(),
			r);
}
 
开发者ID:JavaMoney,项目名称:jsr354-ri-bp,代码行数:19,代码来源:MonetaryOperatorsTest.java

示例8: testPercentBigDecimal

import javax.money.MonetaryAmount; //导入方法依赖的package包/类
/**
 * Test method for
 * {@link org.javamoney.moneta.function.MonetaryOperators#percent(java.math.BigDecimal)}
 * .
 */
@Test
public void testPercentBigDecimal() {
	MonetaryAmount m = Monetary.getDefaultAmountFactory()
			.setCurrency(
					"CHF").setNumber(100L).create();
	MonetaryAmount r = m.with(MonetaryOperators.percent(BigDecimal
			.valueOf(25)));
	assertEquals(
			Monetary.getDefaultAmountFactory().setCurrency("CHF")
					.setNumber(
							new BigDecimal("25")).create(),
			r);
}
 
开发者ID:JavaMoney,项目名称:jsr354-ri-bp,代码行数:19,代码来源:MonetaryOperatorsTest.java

示例9: testPercentNumber

import javax.money.MonetaryAmount; //导入方法依赖的package包/类
/**
 * Test method for
 * {@link org.javamoney.moneta.function.MonetaryOperators#percent(java.lang.Number)}
 * .
 */
@Test
public void testPercentNumber() {
	MonetaryAmount m = Monetary.getDefaultAmountFactory()
			.setCurrency(
					"CHF").setNumber(100).create();
	MonetaryAmount r = m.with(MonetaryOperators.percent((long) 25));
	assertEquals(
			Monetary.getDefaultAmountFactory().setCurrency("CHF")
					.setNumber(
							new BigDecimal("25")).create(),
			r);
}
 
开发者ID:JavaMoney,项目名称:jsr354-ri-bp,代码行数:18,代码来源:MonetaryOperatorsTest.java

示例10: testMinorPart

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

示例11: testMajorPart

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

示例12: testGetRounding

import javax.money.MonetaryAmount; //导入方法依赖的package包/类
/**
 * Test method for {@link javax.money.Monetary#getDefaultRounding()}.
 */
@Test
public void testGetRounding() {
    MonetaryRounding rounding = Monetary.getDefaultRounding();
    assertNotNull(rounding);
    MonetaryAmount m =
            Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(new BigDecimal("10.123456"))
                    .create();
    MonetaryAmount r = m.with(rounding);
    assertEquals(Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(new BigDecimal("10.12"))
            .create(), r);
}
 
开发者ID:JavaMoney,项目名称:jsr354-ri-bp,代码行数:15,代码来源:MonetaryRoundingsTest.java

示例13: testGetRoundingIntRoundingMode

import javax.money.MonetaryAmount; //导入方法依赖的package包/类
/**
 * Test method for
 * {@link javax.money.Monetary#getRounding(javax.money.RoundingQuery)} for arithmetic rounding.
 * .
 */
@Test
public void testGetRoundingIntRoundingMode() {
    MonetaryAmount[] samples = new MonetaryAmount[]{
            Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(new BigDecimal("0.0000000001"))
                    .create(), Monetary.getDefaultAmountFactory().setCurrency("CHF")
            .setNumber(new BigDecimal("1.00000000000023")).create(),
            Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(new BigDecimal("1.1123442323"))
                    .create(),
            Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(new BigDecimal("1.50000000000"))
                    .create(),
            Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(new BigDecimal("-1.000000003"))
                    .create(), Monetary.getDefaultAmountFactory().setCurrency("CHF")
            .setNumber(new BigDecimal("-1.100232876532876389")).create(),
            Monetary.getDefaultAmountFactory().setCurrency("CHF")
                    .setNumber(new BigDecimal("-1.500000000000")).create()};
    int[] scales = new int[]{0, 1, 2, 3, 4, 5};
    for (MonetaryAmount sample : samples) {
        for (int scale : scales) {
            for (RoundingMode roundingMode : RoundingMode.values()) {
                if (roundingMode == RoundingMode.UNNECESSARY) {
                    continue;
                }
                MonetaryOperator rounding = Monetary
                        .getRounding(RoundingQueryBuilder.of().setScale(scale).set(roundingMode).build());
                BigDecimal dec = sample.getNumber().numberValue(BigDecimal.class);
                BigDecimal expected = dec.setScale(scale, roundingMode);
                MonetaryAmount r = sample.with(rounding);
                assertEquals(
                        Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(expected).create(),
                        r);
            }
        }
    }
}
 
开发者ID:JavaMoney,项目名称:jsr354-ri-bp,代码行数:40,代码来源:MonetaryRoundingsTest.java

示例14: testGetRoundingCurrencyUnit

import javax.money.MonetaryAmount; //导入方法依赖的package包/类
/**
 * Test method for
 * {@link javax.money.Monetary#getRounding(javax.money.CurrencyUnit, String...)}
 * .
 */
@Test
public void testGetRoundingCurrencyUnit() {
    MonetaryAmount[] samples = new MonetaryAmount[]{
            Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(new BigDecimal("0.0000000001"))
                    .create(), Monetary.getDefaultAmountFactory().setCurrency("CHF")
            .setNumber(new BigDecimal("1.00000000000023")).create(),
            Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(new BigDecimal("1.1123442323"))
                    .create(),
            Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(new BigDecimal("1.50000000000"))
                    .create(),
            Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(new BigDecimal("-1.000000003"))
                    .create(), Monetary.getDefaultAmountFactory().setCurrency("CHF")
            .setNumber(new BigDecimal("-1.100232876532876389")).create(),
            Monetary.getDefaultAmountFactory().setCurrency("CHF")
                    .setNumber(new BigDecimal("-1.500000000000")).create()};
    for (MonetaryAmount sample : samples) {
        for (Currency currency : Currency.getAvailableCurrencies()) {
            CurrencyUnit cur = Monetary.getCurrency(currency.getCurrencyCode());
            // Omit test roundings, which are for testing only...
            if ("XXX".equals(cur.getCurrencyCode())) {
                continue;
            } else if ("CHF".equals(cur.getCurrencyCode())) {
                continue;
            }
            MonetaryOperator rounding = Monetary.getRounding(cur);
            BigDecimal dec = sample.getNumber().numberValue(BigDecimal.class);
            BigDecimal expected;
            if (cur.getDefaultFractionDigits() < 0) {
                expected = dec.setScale(0, RoundingMode.HALF_UP);
            } else {
                expected = dec.setScale(cur.getDefaultFractionDigits(), RoundingMode.HALF_UP);
            }
            MonetaryAmount r = sample.with(rounding);
            assertEquals(Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(expected).create(),
                    r, "Rouding for: " + sample);
        }
    }
}
 
开发者ID:JavaMoney,项目名称:jsr354-ri-bp,代码行数:44,代码来源:MonetaryRoundingsTest.java


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