Refine search
@Test public void testAmountWithNumberFormat3() { FormattedMoneyHolder3 bean = new FormattedMoneyHolder3(); DataBinder binder = new DataBinder(bean); binder.setConversionService(conversionService); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("amount", "10%"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("10%", binder.getBindingResult().getFieldValue("amount")); assertTrue(bean.getAmount().getNumber().doubleValue() == 0.1d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); }
MonetaryAmount monetaryAmount = Money.of(123.45, euro); CurrencyUnit currency = monetaryAmount.getCurrency(); NumberValue numberValue = monetaryAmount.getNumber(); int intValue = numberValue.intValue(); // 123 double doubleValue = numberValue.doubleValue(); // 123.45 long fractionDenominator = numberValue.getAmountFractionDenominator(); // 100 long fractionNumerator = numberValue.getAmountFractionNumerator(); // 45 int precision = numberValue.getPrecision(); // 5 // NumberValue extends java.lang.Number. // So we assign numberValue to a variable of type Number Number number = numberValue;
/** * 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(); }
@Override public MonetaryAmount apply(MonetaryAmount value) { return value.getFactory() .setNumber(value.getNumber().numberValue(BigDecimal.class).setScale(1, RoundingMode.DOWN)) .create(); } };
/** * Check if a correct double value is returned, truncation is * allowed to be performed. Check should be done for every JDK type * supported. */ @SpecAssertion(section = "4.2.3", id = "423-B9") @Test(description = "4.2.3 Checks if a correct double value is returned, truncation is" + " allowed to be performed. Check should be done for every JDK type.") public void testNumberValueWithTruncationNegative_Double() { Number[] nums = new Number[]{-1213243544435L, -3234, -3.5f - 1.1, -1.2d, -21323234324324.23}; for (Number num : nums) { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount mAmount1 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(num).create(); NumberValue result = mAmount1.getNumber(); AssertJUnit.assertEquals( "Section 4.2.3: Number value (BigDecimal -> double) returned is not correct for " + type.getName(), new BigDecimal(String.valueOf(num)).doubleValue(), result.numberValue(Double.class), 0.0d); AssertJUnit.assertEquals( "Section 4.2.3: Number value (BigDecimal -> double) returned is not correct for " + type.getName(), new BigDecimal(String.valueOf(num)).doubleValue(), result.doubleValue(), 0.0d); } } }
protected static BigDecimal queryFrom(final MonetaryAmount amount, final int fractionsDigit) { Objects.requireNonNull(amount, "Amount required."); BigDecimal number = amount.getNumber().numberValue(BigDecimal.class); number = number.setScale(fractionsDigit, RoundingMode.HALF_EVEN); return number.movePointRight(number.scale()); }
@Override public MonetaryAmount apply(MonetaryAmount amount) { Objects.requireNonNull(amount, "Amount required."); BigDecimal num = amount.getNumber().numberValue(BigDecimal.class).setScale(scale, roundingMode); long minors = num.movePointRight(num.scale()).longValueExact(); int minimalMinors = this.context.getInt(MINMINORS_KEY); long factor = minors / minimalMinors; return amount.getFactory().setCurrency(amount.getCurrency()) .setNumber(BigDecimal.valueOf(minors).movePointLeft(scale)).create();
NumberValue result = mAmount1.getNumber(); for (Class numType : requiredJdkTykes) { if (Byte.class.equals(numType)) { AssertJUnit.assertEquals("Section 4.2.3: Truncating conversion to byte failed for type " + type.getName(), dec.byteValue(), result.byteValue()); } else if (Short.class.equals(numType)) { AssertJUnit.assertEquals("Section 4.2.3: Truncating conversion to short failed for type " + type.getName(), dec.shortValue(), result.shortValue()); } else if (Integer.class.equals(numType)) { AssertJUnit.assertEquals("Section 4.2.3: Truncating conversion to int failed for type " + type.getName(), dec.intValue(), result.intValue()); } else if (Long.class.equals(numType)) { AssertJUnit.assertEquals("Section 4.2.3: Truncating conversion to long failed for type " + type.getName(), dec.longValue(), result.longValue()); } else if (Float.class.equals(numType)) { AssertJUnit.assertEquals("Section 4.2.3: Truncating conversion to float failed for type " + type.getName(), dec.floatValue(), result.floatValue(), 0.0f); } else if (Double.class.equals(numType)) { AssertJUnit.assertEquals("Section 4.2.3: Truncating conversion to double failed for type " + type.getName(), dec.doubleValue(), result.doubleValue(), 0.0d); } else if (BigDecimal.class.equals(numType)) { AssertJUnit result.numberValue(BigDecimal.class).stripTrailingZeros()); } else if (BigInteger.class.equals(numType)) { AssertJUnit .assertEquals("Section 4.2.3: Truncating conversion to BigInteger failed for type " + type.getName(), dec.toBigInteger(), result.numberValue(BigInteger.class));
/** * Extract the minor part of a {@code MonetaryAmount} with the same scale. * <p> * This returns the monetary amount in terms of the minor units of the * currency, truncating the whole part if necessary. For example, 'EUR 2.35' * will return 35, and 'BHD -1.345' will return -345. * <p> * This is returned as a {@code MonetaryAmount} rather than a * {@code BigDecimal} . This is to allow further calculations to be * performed on the result. Should you need a {@code BigDecimal}, simply * call {@code asType(BigDecimal.class)}. * @return the minor units part of the amount, never {@code null} */ @Override public Long queryFrom(MonetaryAmount amount) { Objects.requireNonNull(amount, "Amount required."); int fractionDigits = amount.getCurrency().getDefaultFractionDigits(); BigDecimal number = amount.getNumber().numberValue(BigDecimal.class); return number.setScale(fractionDigits, RoundingMode.DOWN) .remainder(BigDecimal.ONE) .movePointRight(fractionDigits).longValue(); }
@Override public MonetaryAmount apply(MonetaryAmount amount) { RoundedMoney roundedMoney = RoundedMoney.from(Objects.requireNonNull(amount)); BigDecimal numberValue = roundedMoney.getNumber().numberValue(BigDecimal.class); BigDecimal numberRounded = numberValue.setScale(scale, roundingMode); return RoundedMoney.of(numberRounded, roundedMoney.getCurrency(), this); }
@Override public int compareTo(MonetaryAmount o) { Objects.requireNonNull(o); int compare = getCurrency().getCurrencyCode().compareTo(o.getCurrency().getCurrencyCode()); if (compare == 0) { compare = getNumber().numberValue(BigDecimal.class).compareTo(o.getNumber().numberValue(BigDecimal.class)); } return compare; }
/** * Gets the minor part of a {@code MonetaryAmount} with the same scale. * <p> * This returns the monetary amount in terms of the minor units of the * currency, truncating the whole part if necessary. For example, 'EUR 2.35' * will return 'EUR 0.35', and 'BHD -1.345' will return 'BHD -0.345'. * <p> * This is returned as a {@code MonetaryAmount} rather than a * {@code BigDecimal} . This is to allow further calculations to be * performed on the result. Should you need a {@code BigDecimal}, simply * call {@code asType(BigDecimal.class)}. * * @return the minor units part of the amount, never {@code null} */ @Override public MonetaryAmount apply(MonetaryAmount amount){ Objects.requireNonNull(amount, "Amount required."); BigDecimal number = amount.getNumber().numberValue(BigDecimal.class); BigDecimal wholes = number.setScale(0, RoundingMode.DOWN); return amount.subtract(amount.getFactory().setNumber(wholes).create()); }
BigDecimal bd = new BigDecimal(num); try { mAmount1 = NumberValue result = mAmount1.getNumber(); AssertJUnit.assertEquals("Section 4.2.3: Amount's scale does not match for " + bd + " correct for " + type.getName(), bd.precision(), result.getPrecision());
@Override public MonetaryAmount apply(MonetaryAmount amount) { RoundedMoney roundedMoney = RoundedMoney.from(Objects.requireNonNull(amount)); BigDecimal numberValue = roundedMoney.getNumber().numberValue(BigDecimal.class); BigDecimal numberRounded = numberValue.round(mathContext); return RoundedMoney.of(numberRounded, roundedMoney.getCurrency(), this); }
/** * Gets the amount in major units as a {@code long}. * <p> * This returns the monetary amount in terms of the major units of the * currency, truncating the amount if necessary. For example, 'EUR 2.35' * will return 2, and 'BHD -1.345' will return -1. * <p> * This method matches the API of {@link java.math.BigDecimal}. * * @return the major units part of the amount * @throws ArithmeticException if the amount is too large for a {@code long} */ @Override public Long queryFrom(MonetaryAmount amount) { Objects.requireNonNull(amount, "Amount required."); return amount.with(downRounding).getNumber().longValueExact(); } }
/** * Creates a new instance os {@link FastMoney}. * * @param currency the currency, not null. * @param numberValue the numeric value, not null. */ private FastMoney(NumberValue numberValue, CurrencyUnit currency, boolean allowInternalRounding) { Objects.requireNonNull(currency, "Currency is required."); this.currency = currency; Objects.requireNonNull(numberValue, "Number is required."); this.number = getInternalNumber(numberValue.numberValue(BigDecimal.class), allowInternalRounding); }
/** * Check if a correct double value is returned, truncation is * allowed to be performed. Check should be done for every JDK type * supported. */ @SpecAssertion(section = "4.2.3", id = "423-B9") @Test(description = "4.2.3 Checks if a correct int value is returned, truncation is" + " allowed to be performed. Check should be done for every JDK type.") public void testNumberValueWithTruncationNegative_Integer() { Number[] nums = new Number[]{-1213243544435L, -3234, -3.5f - 1.1, -1.2d, -21323234324324.23}; for (Number num : nums) { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount mAmount1 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(num).create(); NumberValue result = mAmount1.getNumber(); AssertJUnit .assertEquals("Section 4.2.3: Number value (BigDecimal -> byte) returned is not correct for " + type.getName(), (long) new BigDecimal(String.valueOf(num)).intValue(), (long) result.numberValue(Integer.class)); AssertJUnit .assertEquals("Section 4.2.3: Number value (BigDecimal -> byte) returned is not correct for " + type.getName(), (long) new BigDecimal(String.valueOf(num)).intValue(), (long) result.intValue()); } } }
" allowed to be performed.") public void testNumberValueZero() { Number[] nums = new Number[]{0.0, -0.0, new BigDecimal("0.00000"), new BigDecimal("-0.000000000000000000000"), new BigInteger("0")}; for (Number num : nums) { NumberValue result = mAmount1.getNumber(); AssertJUnit.assertEquals("Section 4.2.3: Number value (Number, long) returned is not correct for " + num + ", type; " + type.getName(), 0L, result.numberValue(BigDecimal.class).longValueExact()); AssertJUnit .assertEquals("Section 4.2.3: Number value (Number, short) returned is not correct for " + num + ", type; " + type.getName(), 0.0f, result.numberValue(Short.class).floatValue(), 0.0f); AssertJUnit.assertEquals("Section 4.2.3: Number value (Number, int) returned is not correct for " + num + ", type; " + type.getName(), 0, result.numberValue(Short.class).intValue()); AssertJUnit .assertEquals("Section 4.2.3: Number value (Number, double) returned is not correct for " + num + ", type; " + type.getName(), 0.0d, result.numberValue(Double.class), 0.0f); AssertJUnit.assertEquals( "Section 4.2.3: Number value (Number, BigInteger) returned is not correct for " + num + ", type; " + type.getName(), 0L, result.numberValue(BigInteger.class).longValue()); AssertJUnit.assertEquals( "Section 4.2.3: Number value (Number, BigDecimal) returned is not correct for " + num + ", type; " + type.getName(), 0L, result.numberValue(BigDecimal.class).longValueExact()); result.numberValueExact(BigDecimal.class);
f.setCurrency("CHF"); MonetaryAmount[] moneys = new MonetaryAmount[]{f.setNumber(100).create(), f.setNumber(new BigDecimal("23123213.435")).create(), f.setNumber(new BigDecimal("-23123213.435")).create(), f.setNumber(-23123213).create(), f.setNumber(0).create()}; BigDecimal[] numbers = new BigDecimal[]{new BigDecimal("100"), new BigDecimal("23123213.435"), new BigDecimal("-23123213.435"), new BigDecimal("-23123213"), BigDecimal.ZERO}; int[] intNums = new int[]{100, 23123213, -23123213, -23123213, 0}; NumberValue nv = moneys[i].getNumber(); AssertJUnit.assertNotNull("Section 4.2.2: Amount returned returns null for getNumber(), type: " + moneys[i].getClass().getName(), nv); AssertJUnit.assertEquals( "Section 4.2.2: getNumber().numberValue(BigDecimal.class) incorrect for " + type.getName(), numbers[i].stripTrailingZeros(), nv.numberValue(BigDecimal.class).stripTrailingZeros()); AssertJUnit.assertEquals("Section 4.2.2: getNumber().intValue() incorrect for " + type.getName(), intNums[i], nv.intValue()); AssertJUnit.assertEquals("Section 4.2.2: getNumber().longValue() incorrect for " + type.getName(), longNums[i], nv.longValue()); AssertJUnit.assertEquals("Section 4.2.2: getNumber().doubleValue() incorrect for " + type.getName(), doubleNums[i], nv.doubleValue(), 0.0d); AssertJUnit.assertEquals("Section 4.2.2: getNumber().floatValue() incorrect for " + type.getName(), floatNums[i], nv.floatValue(), 0.0d);
@Override public int hashCode() { // Numerically equal factors should produce the same hash code, so hash a normalized // version of the factor rather than the NumberValue object. BigDecimal normalizedFactor = factor.numberValue(BigDecimal.class).stripTrailingZeros(); // The exchange rate chain includes a reference to "this" if the caller doesn't explicitly // set a chain in the builder, so we can't naively hash the chain or we'll get infinite // recursion. int chainHash = 0; for (ExchangeRate chainedRate : chain) { if (chainedRate == this) { // Use a constant to represent the presence of this object in the chain. chainHash = Objects.hash(chainHash, "this"); } else { chainHash = Objects.hash(chainHash, chainedRate); } } return Objects.hash(base, conversionContext, normalizedFactor, term, chainHash); }