RoundingUtil { public static BigDecimal round(BigDecimal value, Integer nearest, CashierOptions.RoundingMode mode) { if (nearest == null || nearest.equals(0)) { return value; } double valueD = value.doubleValue(); switch (mode) { case FLOOR: return new BigDecimal(nearest * (Math.floor(Math.abs(valueD / nearest)))); case CEILING: return new BigDecimal(nearest * (Math.ceil(Math.abs(valueD / nearest)))); case MID: return new BigDecimal(nearest * (Math.round(valueD / nearest))); default: return value; } } protected  RoundingUtil(); static BigDecimal round(BigDecimal value, Integer nearest, CashierOptions.RoundingMode mode); static void setupRoundingDeptAndItem(Log log); static void handleRoundingLineItem(Bill bill);  }
@Test public void roundBillTotal_shouldRoundToNearest() throws Exception { Assert.assertEquals(new BigDecimal(5), RoundingUtil.round(new BigDecimal(3), 5, CashierOptions.RoundingMode.MID)); Assert.assertEquals(new BigDecimal(10), RoundingUtil.round(new BigDecimal(12), 5, CashierOptions.RoundingMode.MID)); }
@Test public void roundBillTotal_shouldRoundToNearestCeiling() throws Exception { Assert.assertEquals(new BigDecimal(5), RoundingUtil.round(new BigDecimal(1), 5, CashierOptions.RoundingMode.CEILING)); BigDecimal decimal = new BigDecimal(BigInteger.ONE, 2); Assert.assertEquals(new BigDecimal(1), RoundingUtil.round(decimal, 1, CashierOptions.RoundingMode.CEILING)); }
@Test public void roundBillTotal_shouldRoundToNearestFloor() throws Exception { Assert.assertEquals(new BigDecimal(5), RoundingUtil.round(new BigDecimal(9), 5, CashierOptions.RoundingMode.FLOOR)); BigDecimal decimal = new BigDecimal(BigInteger.valueOf(199), 2); Assert.assertEquals(new BigDecimal(1), RoundingUtil.round(decimal, 1, CashierOptions.RoundingMode.FLOOR)); }