NumberUtils { public static DivisionResult divideWithLoss(Double dividend, Integer divisor, boolean resultToBeNegative) { DivisionResult result = new DivisionResult(); Double dividendHere = Math.abs(dividend); Double divisionResult = NumberUtils.divide(dividendHere, Integer.valueOf(divisor)); Double loss = NumberUtils.round(dividendHere - NumberUtils.multiply(divisionResult, divisor)); if (resultToBeNegative) { divisionResult = NumberUtils.neg(divisionResult); loss = NumberUtils.neg(loss); } result.setLoss(loss); result.setResult(divisionResult); return result; }  static Double neg(Double value); static Double getStringToDoubleNonRounded(Locale locale,
                                                     String stringToBeParsed); static Double getStringToDoubleRounded(Locale locale,
            String stringToBeParsed); static Double divide(Double dividend, Integer divisor); static DivisionResult divideWithLoss(Double dividend,
            Integer divisor, boolean resultToBeNegative); static Double multiply(Double factorA, Integer factorB); static Double multiply(Double factorA, Double factorB); static Double divide(Double dividend, Double divisor); static Double divideForExchangeRates(Double dividend, Double divisor); static Double ensureExchangeRateMinMax(Double double1); static Double ensureAmountMinMax(Double double1); static boolean isExceedingAmountLimit(Double double1); static Double invertExchangeRateDouble(Double exchangeRate); static Double divide(Double dividend, Double divisor, int scale); static double round(double nonRounded);  }
@Test public void testDivisonWithLoss() { DivisionResult result; result = NumberUtils.divideWithLoss(-100d, 3, true); Assert.assertEquals(Double.valueOf(-33.33d), result.getResult()); Assert.assertEquals(Double.valueOf(-0.01d), result.getLoss()); result = NumberUtils.divideWithLoss(-50d, 3, true); Assert.assertEquals(Double.valueOf(-16.67d), result.getResult()); Assert.assertEquals(Double.valueOf(0.01d), result.getLoss()); result = NumberUtils.divideWithLoss(-100d, 3, false); Assert.assertEquals(Double.valueOf(33.33d), result.getResult()); Assert.assertEquals(Double.valueOf(0.01d), result.getLoss()); result = NumberUtils.divideWithLoss(-50d, 3, false); Assert.assertEquals(Double.valueOf(16.67d), result.getResult()); Assert.assertEquals(Double.valueOf(-0.01d), result.getLoss()); result = NumberUtils.divideWithLoss(100d, 3, true); Assert.assertEquals(Double.valueOf(-33.33d), result.getResult()); Assert.assertEquals(Double.valueOf(-0.01d), result.getLoss()); result = NumberUtils.divideWithLoss(50d, 3, true); Assert.assertEquals(Double.valueOf(-16.67d), result.getResult()); Assert.assertEquals(Double.valueOf(0.01d), result.getLoss()); result = NumberUtils.divideWithLoss(10000d, 3, true); Assert.assertEquals(Double.valueOf(-3333.33d), result.getResult()); Assert.assertEquals(Double.valueOf(-0.01d), result.getLoss()); result = NumberUtils.divideWithLoss(100000d, 3, true); Assert.assertEquals(Double.valueOf(-33333.33d), result.getResult()); Assert.assertEquals(Double.valueOf(-0.01d), result.getLoss()); result = NumberUtils.divideWithLoss(1000000d, 3, true); Assert.assertEquals(Double.valueOf(-333333.33d), result.getResult()); Assert.assertEquals(Double.valueOf(-0.01d), result.getLoss()); result = NumberUtils.divideWithLoss(10000000d, 3, true); Assert.assertEquals(Double.valueOf(-3333333.33d), result.getResult()); Assert.assertEquals(Double.valueOf(-0.01d), result.getLoss()); result = NumberUtils.divideWithLoss(100000000d, 3, true); Assert.assertEquals(Double.valueOf(-33333333.33d), result.getResult()); Assert.assertEquals(Double.valueOf(-0.01d), result.getLoss()); result = NumberUtils.divideWithLoss(999999999999d, 3, true); Assert.assertEquals(Double.valueOf(-333333333333d), result.getResult()); Assert.assertEquals(Double.valueOf(-0d), result.getLoss()); result = NumberUtils.divideWithLoss(999999999.99d, 3, true); Assert.assertEquals(Double.valueOf(-333333333.33d), result.getResult()); Assert.assertEquals(Double.valueOf(-0d), result.getLoss()); result = NumberUtils.divideWithLoss(999999999.98d, 3, true); Assert.assertEquals(Double.valueOf(-333333333.33d), result.getResult()); Assert.assertEquals(Double.valueOf(0.01d), result.getLoss()); }