本文整理汇总了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);
}
示例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);
}
}
示例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
}
示例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
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
}
}
}
示例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);
}
}
}