@Test(expectedExceptions = ArithmeticException.class) public void shouldReturnWhenTheValueIsBiggerThanLong() { CurrencyUnit real = Monetary.getCurrency("BRL"); MonetaryAmount monetaryAmount = Money.of(Long.MAX_VALUE, real); query.queryFrom(monetaryAmount.add(Money.of(10, real))); fail(); }
@Override public Long queryFrom(MonetaryAmount amount) { Objects.requireNonNull(amount, "Amount required."); BigDecimal number = amount.getNumber().numberValue(BigDecimal.class); CurrencyUnit cur = amount.getCurrency(); int scale = cur.getDefaultFractionDigits(); if(scale<0){ scale = 0; } number = number.setScale(scale, RoundingMode.DOWN); return number.movePointRight(number.scale()).longValueExact(); }
ConvertMinorPartQuery implements MonetaryQuery<Long> { @Override public Long queryFrom(MonetaryAmount amount) { Objects.requireNonNull(amount, "Amount required."); BigDecimal number = amount.getNumber().numberValue(BigDecimal.class); CurrencyUnit cur = amount.getCurrency(); int scale = cur.getDefaultFractionDigits(); if(scale<0){ scale = 0; } number = number.setScale(scale, RoundingMode.DOWN); return number.movePointRight(number.scale()).longValueExact(); } }
ConvertMinorPartQuery implements MonetaryQuery<Long> { @Override public Long queryFrom(MonetaryAmount amount) { Objects.requireNonNull(amount, "Amount required."); BigDecimal number = amount.getNumber().numberValue(BigDecimal.class); CurrencyUnit cur = amount.getCurrency(); int scale = cur.getDefaultFractionDigits(); if(scale<0){ scale = 0; } number = number.setScale(scale, RoundingMode.DOWN); return number.movePointRight(number.scale()).longValueExact(); } ConvertMinorPartQuery(); }
ConvertMinorPartQuery implements MonetaryQuery<Long> { @Override public Long queryFrom(MonetaryAmount amount) { Objects.requireNonNull(amount, "Amount required."); BigDecimal number = amount.getNumber().numberValue(BigDecimal.class); CurrencyUnit cur = amount.getCurrency(); int scale = cur.getDefaultFractionDigits(); if(scale<0){ scale = 0; } number = number.setScale(scale, RoundingMode.DOWN); return number.movePointRight(number.scale()).longValueExact(); } ConvertMinorPartQuery(); @Override Long queryFrom(MonetaryAmount amount); }
ConvertMinorPartQuery implements MonetaryQuery<Long> { @Override public Long queryFrom(MonetaryAmount amount) { Objects.requireNonNull(amount, "Amount required."); BigDecimal number = amount.getNumber().numberValue(BigDecimal.class); CurrencyUnit cur = amount.getCurrency(); int scale = cur.getDefaultFractionDigits(); if(scale<0){ scale = 0; } number = number.setScale(scale, RoundingMode.DOWN); return number.movePointRight(number.scale()).longValueExact(); } ConvertMinorPartQuery(); @Override Long queryFrom(MonetaryAmount amount);  }