private void checkAmountParameter(MonetaryAmount amount) { MoneyUtils.checkAmountParameter(amount, this.currency); // numeric check for overflow... if (amount.getNumber().getScale() > SCALE) { throw new ArithmeticException("Parameter exceeds maximal scale: " + SCALE); } if (amount.getNumber().getPrecision() > MAX_BD.precision()) { throw new ArithmeticException("Parameter exceeds maximal precision: " + SCALE); } }
/** * Gets the amount as reciprocal / multiplicative inversed value (1/n). * <p> * E.g. 'EUR 2.0' will be converted to 'EUR 0.5'. * * @return the reciprocal / multiplicative inversed of the amount * @throws ArithmeticException if the arithmetic operation failed */ @Override public MonetaryAmount apply(MonetaryAmount amount){ Objects.requireNonNull(amount, "Amount required."); NumberValue num = amount.getNumber(); BigDecimal one = new BigDecimal("1.0").setScale(num.getScale() < 5 ? 5 : num.getScale(), BigDecimal.ROUND_HALF_EVEN); return amount.getFactory().setNumber(one.divide(num.numberValue(BigDecimal.class), RoundingMode.HALF_EVEN)) .create(); }
/** * Check if a correct scale value is returned. For 0 the scale should always be 0. */ @SpecAssertion(section = "4.2.3", id = "423-C10") @Test(description = "4.2.3 Check if a correct scale value is returned. Check should be done for every JDK type " + "supported.") public void testScaleZero() { String[] nums = new String[]{"-0", "-0.0", "-0.00", "-0.000", "-0.0000", "-0.00000", "-0.000000", "-0.00000000"}; for (String num : nums) { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount mAmount1; BigDecimal bd = new BigDecimal(num); try { mAmount1 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(bd).create(); } catch (MonetaryException | ArithmeticException e) { // It is possible, that our test may exceed the capabilities, so in that case, we just continue continue; } NumberValue result = mAmount1.getNumber(); AssertJUnit.assertTrue("Section 4.2.3: Amount's scale is < 0 for " + num + ", was " + result.getScale() + " for " + type.getName(), 0 <= result.getScale()); } } }
/** * Test correct scale values, including border cases. */ @SpecAssertion(section = "4.2.3", id = "423-B11") @Test(description = "4.2.3 Test correct scale values, including border cases.") public void testScaleNegative() { String[] nums = new String[]{"-1.12", "-1.12", "-1.123", "-1.1234", "-1.12345", "-1.123456", "-1.1234567", "-1.12345678", "-1.123456789", "-12.12", "-123.12", "-1234.123", "-12345.1234", "-123456.12345", "-123456.123456", "-12345678.1234567", "-12345678.12345678", "-123456789.123456789", "-1", "-12", "-123", "-1234", "-12345", "-123456", "-1234567", "-12345678", "-123456789"}; for (String num : nums) { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount mAmount1; BigDecimal bd = new BigDecimal(num); try { mAmount1 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(bd).create(); } catch (MonetaryException | ArithmeticException e) { // It is possible, that our test may exceed the capabilities, so in that case, we just continue continue; } NumberValue result = mAmount1.getNumber(); AssertJUnit.assertEquals("Section 4.2.3: Amount's scale does not match for " + bd + " correct for " + type.getName(), bd.scale(), result.getScale()); } } }
AssertJUnit .assertEquals("Section 4.2.3: Amount's precision does not match for " + bd + " correct for " + type.getName(), bd.scale(), result.getScale());