本文整理汇总了Java中javax.money.MonetaryOperator类的典型用法代码示例。如果您正苦于以下问题:Java MonetaryOperator类的具体用法?Java MonetaryOperator怎么用?Java MonetaryOperator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MonetaryOperator类属于javax.money包,在下文中一共展示了MonetaryOperator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: apply
import javax.money.MonetaryOperator; //导入依赖的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);
}
}
示例2: main
import javax.money.MonetaryOperator; //导入依赖的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
}
示例3: RoundedMoney
import javax.money.MonetaryOperator; //导入依赖的package包/类
@Deprecated
public RoundedMoney(Number number, CurrencyUnit currency, MonetaryContext context, MonetaryOperator rounding) {
Objects.requireNonNull(currency, "Currency is required.");
this.currency = currency;
Objects.requireNonNull(number, "Number is required.");
checkNumber(number);
MonetaryContextBuilder monetaryContextBuilder = DEFAULT_MONETARY_CONTEXT.toBuilder();
if (context != null) {
monetaryContextBuilder.importContext(context);
}
this.rounding = RoundedMoneyMonetaryOperatorFactory.INSTANCE.getDefaultMonetaryOperator(rounding, monetaryContextBuilder.build());
monetaryContextBuilder.set(MONETARY_ROUNDING_KEY, this.rounding);
this.monetaryContext = monetaryContextBuilder.build();
this.number = MoneyUtils.getBigDecimal(number, monetaryContext);
}
示例4: testGetCashRoundingCurrencyUnit
import javax.money.MonetaryOperator; //导入依赖的package包/类
/**
* Test method for
* {@link javax.money.Monetary#getRounding(javax.money.CurrencyUnit, String...)} for cash ropundings.
* .
*/
@Test
public void testGetCashRoundingCurrencyUnit() {
MonetaryOperator r = Monetary.getRounding(
RoundingQueryBuilder.of().setCurrency(Monetary.getCurrency("GBP")).set("cashRounding", true)
.build());
assertNotNull(r);
r = Monetary.getRounding(
RoundingQueryBuilder.of().setCurrency(Monetary.getCurrency("CHF")).set("cashRounding", true)
.build());
assertNotNull(r);
assertEquals(Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(2).create(),
Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(2.02).create().with(r));
assertEquals(Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(2.05).create(),
Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(2.025).create().with(r));
}
示例5: shouldReturnMathContextOperator
import javax.money.MonetaryOperator; //导入依赖的package包/类
@Test
public void shouldReturnMathContextOperator() {
int precision = 6;
RoundingMode roundingMode = RoundingMode.HALF_EVEN;
MonetaryRoundedFactory factory = MonetaryRoundedFactory
.withRoundingMode(roundingMode).withPrecision(precision)
.build();
assertNotNull(factory);
MonetaryOperator roundingOperator = factory.getRoundingOperator();
assertNotNull(roundingOperator);
assertTrue(PrecisionContextRoundedOperator.class.isInstance(roundingOperator));
MathContext result = PrecisionContextRoundedOperator.class.cast(roundingOperator).getMathContext();
assertEquals(precision, result.getPrecision());
assertEquals(roundingMode, result.getRoundingMode());
}
示例6: shouldReturnScaleRoundingOperator
import javax.money.MonetaryOperator; //导入依赖的package包/类
@Test
public void shouldReturnScaleRoundingOperator() {
int scale = 6;
RoundingMode roundingMode = RoundingMode.HALF_EVEN;
MonetaryRoundedFactory factory = MonetaryRoundedFactory
.withRoundingMode(roundingMode).withScale(scale)
.build();
assertNotNull(factory);
MonetaryOperator roundingOperator = factory.getRoundingOperator();
assertNotNull(roundingOperator);
assertTrue(ScaleRoundedOperator.class.isInstance(roundingOperator));
ScaleRoundedOperator result = ScaleRoundedOperator.class.cast(roundingOperator);
assertEquals(scale, result.getScale());
assertEquals(roundingMode, result.getRoundingMode());
}
示例7: shouldReturnMathContextScaleOperator
import javax.money.MonetaryOperator; //导入依赖的package包/类
@Test
public void shouldReturnMathContextScaleOperator() {
int precision = 6;
int scale = 3;
RoundingMode roundingMode = RoundingMode.HALF_EVEN;
MonetaryRoundedFactory factory = MonetaryRoundedFactory
.withRoundingMode(roundingMode).withPrecision(precision).withScale(scale)
.build();
assertNotNull(factory);
MonetaryOperator roundingOperator = factory.getRoundingOperator();
assertNotNull(roundingOperator);
assertTrue(PrecisionScaleRoundedOperator.class.isInstance(roundingOperator));
PrecisionScaleRoundedOperator result = PrecisionScaleRoundedOperator.class.cast(roundingOperator);
assertEquals(precision, result.getMathContext().getPrecision());
assertEquals(scale, result.getScale());
assertEquals(roundingMode, result.getMathContext().getRoundingMode());
}
示例8: shouldReturnScaleMathContextOperator
import javax.money.MonetaryOperator; //导入依赖的package包/类
@Test
public void shouldReturnScaleMathContextOperator() {
int precision = 6;
int scale = 3;
RoundingMode roundingMode = RoundingMode.HALF_EVEN;
MonetaryRoundedFactory factory = MonetaryRoundedFactory
.withRoundingMode(roundingMode).withScale(scale).withPrecision(precision)
.build();
assertNotNull(factory);
MonetaryOperator roundingOperator = factory.getRoundingOperator();
assertNotNull(roundingOperator);
assertTrue(PrecisionScaleRoundedOperator.class.isInstance(roundingOperator));
PrecisionScaleRoundedOperator result = PrecisionScaleRoundedOperator.class.cast(roundingOperator);
assertEquals(precision, result.getMathContext().getPrecision());
assertEquals(scale, result.getScale());
assertEquals(roundingMode, result.getMathContext().getRoundingMode());
}
示例9: otherOperations
import javax.money.MonetaryOperator; //导入依赖的package包/类
@Test
public void otherOperations() {
CurrencyUnit euro = Monetary.getCurrency("EUR");
MonetaryAmount amount = FastMoney.of(150, euro);
FastMoney money = FastMoney.of(150, "EUR");
assertEquals(amount, money);
FastMoney third = money.divide(3);
assertEquals(third, FastMoney.of(50, euro));
MonetaryOperator tenPercent = MonetaryOperators.percent(10);
FastMoney tenPercentValue = money.with(tenPercent);
assertEquals(tenPercentValue, FastMoney.of(15, "EUR"));
// excellent comparison functions:
//money.isZero()
//MonetaryFunctions
Log.d("test", "the end, debug manually");
}
示例10: testOfAndApply
import javax.money.MonetaryOperator; //导入依赖的package包/类
/**
* Method: of(Rate rate, int periods)
*/
@Test
public void testOfAndApply() throws Exception {
Money money = Money.of(100, "CHF");
MonetaryOperator rounding = Monetary.getRounding(RoundingQueryBuilder.of().setScale(2).set(RoundingMode.HALF_EVEN)
.build());
assertEquals(Money.of(BigDecimal.valueOf(95.12), "CHF"), money.with(PresentValueContinuousCompounding.of(Rate.of(0.05), 1)).with(rounding));
assertEquals(Money.of(BigDecimal.valueOf(90.48), "CHF"), money.with(PresentValueContinuousCompounding.of(Rate.of(0.05), 2)).with(rounding));
assertEquals(Money.of(BigDecimal.valueOf(46.3), "CHF"), money.with(PresentValueContinuousCompounding.of(Rate.of(0.07), 11)).with(rounding));
assertEquals(Money.of(BigDecimal.valueOf(100.00), "CHF"), money.with(PresentValueContinuousCompounding.of(Rate.of(0.05), 0)).with(rounding));
assertEquals(Money.of(BigDecimal.valueOf(100.00), "CHF"), money.with(PresentValueContinuousCompounding.of(Rate.of(-0.05), 0)).with(rounding));
assertEquals(Money.of(BigDecimal.valueOf(105.13), "CHF"), money.with(PresentValueContinuousCompounding.of(Rate.of(-0.05), 1)).with(rounding));
assertEquals(Money.of(BigDecimal.valueOf(110.52), "CHF"), money.with(PresentValueContinuousCompounding.of(Rate.of(-0.05), 2)).with(rounding));
assertEquals(Money.of(BigDecimal.valueOf(215.98), "CHF"), money.with(PresentValueContinuousCompounding.of(Rate.of(-0.07), 11)).with(rounding));
}
示例11: testWithMonetaryOperator
import javax.money.MonetaryOperator; //导入依赖的package包/类
/**
* Test method for {@link org.javamoney.moneta.FastMoney#with(javax.money.MonetaryOperator)} .
*/
@Test
public void testWithMonetaryOperator() {
MonetaryOperator adj = amount -> FastMoney.of(-100, amount.getCurrency());
FastMoney m = FastMoney.of(new BigDecimal("1.2345"), "XXX");
FastMoney a = m.with(adj);
assertNotNull(a);
assertNotSame(m, a);
assertEquals(m.getCurrency(), a.getCurrency());
assertEquals(FastMoney.of(-100, m.getCurrency()), a);
adj = amount -> amount.multiply(2).getFactory().setCurrency(Monetary.getCurrency("CHF")).create();
a = m.with(adj);
assertNotNull(a);
assertNotSame(m, a);
assertEquals(Monetary.getCurrency("CHF"), a.getCurrency());
assertEquals(FastMoney.of(1.2345 * 2, a.getCurrency()), a);
}
示例12: main
import javax.money.MonetaryOperator; //导入依赖的package包/类
public static void main(String[] args) {
MonetaryOperator doubleOperator = m -> m.multiply(2);
MonetaryOperator halfOperator = m -> m.divide(2);
MonetaryOperator operator = m -> {
if(m.isPositive()){
return m.multiply(2);
}
return m.divide(2);
};
}
示例13: testPercentToString
import javax.money.MonetaryOperator; //导入依赖的package包/类
/**
* Test method for
* {@link org.javamoney.moneta.function.MonetaryOperators#percent(java.lang.Number)}
* .
*/
@Test
public void testPercentToString() {
MonetaryOperator p = MonetaryOperators.percent((short) 25);
assertTrue(p.toString().contains("25"));
assertTrue(p.toString().contains("%"));
}
示例14: testGetRoundingIntRoundingMode
import javax.money.MonetaryOperator; //导入依赖的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);
}
}
}
}
示例15: testGetRoundingCurrencyUnit
import javax.money.MonetaryOperator; //导入依赖的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);
}
}
}