MoneyUtils { public static BigDecimal getDiscountDisplayValue(final BigDecimal original, final BigDecimal discounted) { if (original == null || discounted == null) { return BigDecimal.ZERO; } return original.subtract(discounted).multiply(HUNDRED) .divide(original, RoundingMode.FLOOR).setScale(0, RoundingMode.HALF_EVEN); } private  MoneyUtils(); static BigDecimal max(final BigDecimal first, final BigDecimal second); static BigDecimal secondOrFirst(final Pair<BigDecimal, BigDecimal> firstAndSecond); static BigDecimal minPositive(final Pair<BigDecimal, BigDecimal> firstAndSecond); static BigDecimal minPositive(final BigDecimal first, final BigDecimal second); static BigDecimal notNull(final BigDecimal value); static BigDecimal notNull(final BigDecimal value, final BigDecimal ifNull); static boolean isPositive(final BigDecimal value); static boolean isFirstBiggerThanSecond(final BigDecimal first, final BigDecimal second); static boolean isFirstBiggerThanOrEqualToSecond(final BigDecimal first, final BigDecimal second); static boolean isFirstEqualToSecond(final BigDecimal first, final BigDecimal second); static boolean isFirstEqualToSecond(final BigDecimal first, final BigDecimal second, final int scale); static BigDecimal getDiscountDisplayValue(final BigDecimal original, final BigDecimal discounted); static Money getMoney(final BigDecimal money, final BigDecimal taxRate, final boolean taxIncluded); static BigDecimal getTaxAmount(final BigDecimal money, final BigDecimal taxRate, final boolean taxIncluded); static BigDecimal getNetAmount(final BigDecimal money, final BigDecimal taxRate, final boolean taxIncluded); static BigDecimal getGrossAmount(final BigDecimal money, final BigDecimal taxRate, final boolean taxIncluded); static final BigDecimal ZERO; static final BigDecimal ONE; static final BigDecimal HUNDRED; }
@Test public void testGetDiscountDisplayValueOnNullPrices() throws Exception { assertSame(BigDecimal.ZERO, MoneyUtils.getDiscountDisplayValue(null, null)); assertSame(BigDecimal.ZERO, MoneyUtils.getDiscountDisplayValue(BigDecimal.TEN, null)); assertSame(BigDecimal.ZERO, MoneyUtils.getDiscountDisplayValue(null, BigDecimal.TEN)); }
@Test public void testGetDiscountDisplayValue() throws Exception { assertEquals("20", MoneyUtils.getDiscountDisplayValue(new BigDecimal("100.00"), new BigDecimal("80.00")).toPlainString()); assertEquals("0", MoneyUtils.getDiscountDisplayValue(new BigDecimal("100.00"), new BigDecimal("100.00")).toPlainString()); assertEquals("-20", MoneyUtils.getDiscountDisplayValue(new BigDecimal("100.00"), new BigDecimal("120.00")).toPlainString()); assertEquals("19", MoneyUtils.getDiscountDisplayValue(new BigDecimal("100.00"), new BigDecimal("80.99")).toPlainString()); }