/** * Creates an amount with the given precision. * @param precision the target precision * @return a corresponding amount. */ public static MonetaryAmount createAmountWithPrecision(int precision) { MonetaryAmountFactoryQuery tgtContext = MonetaryAmountFactoryQueryBuilder.of().setPrecision(precision).build(); MonetaryAmountFactory<?> exceedingFactory; try { exceedingFactory = Monetary.getAmountFactory(tgtContext); AssertJUnit.assertNotNull(exceedingFactory); MonetaryAmountFactory<? extends MonetaryAmount> bigFactory = Monetary.getAmountFactory(exceedingFactory.getAmountType()); return bigFactory.setCurrency("CHF").setNumber(createNumberWithPrecision(precision)).create(); } catch (MonetaryException e) { return null; } } }
/** * @param args */ public static void main(String[] args) { MonetaryAmount amount = Monetary.getDefaultAmountFactory().setCurrency("EUR").setNumber(234).create(); ConsoleUtils.printDetails(amount); amount = Monetary.getAmountFactory(FastMoney.class).setCurrency("EUR").setNumber(234).create(); ConsoleUtils.printDetails(amount); amount = Monetary.getAmountFactory( MonetaryAmountFactoryQueryBuilder.of().setMaxScale(50).setPrecision(30).build()) .setCurrency("EUR").setNumber(234).create(); ConsoleUtils.printDetails(amount); Money amt1 = Money.of(10.1234556123456789, "USD"); FastMoney amt2 = FastMoney.of(123456789, "USD"); Money total = amt1.add(amt2).multiply(0.5) .remainder(1); ConsoleUtils.printDetails(total); }