/** * Obtains an instance from the number of decimal places and fraction. * <p> * This returns a convention that rounds to a fraction of the specified number of decimal places. * Rounding follows the normal {@link RoundingMode#HALF_UP} convention. * <p> * For example, to round to the nearest 1/32nd of the 4th decimal place, call * this method with the arguments 4 and 32. * * @param decimalPlaces the number of decimal places to round to, from 0 to 255 inclusive * @param fraction the fraction of the last decimal place, such as 32 for 1/32, from 0 to 255 inclusive * @return the rounding convention * @throws IllegalArgumentException if the decimal places or fraction is invalid */ public static Rounding ofFractionalDecimalPlaces(int decimalPlaces, int fraction) { return HalfUpRounding.ofFractionalDecimalPlaces(decimalPlaces, fraction); }
public void test_ofFractionalDecimalPlaces_invalid() { assertThrowsIllegalArg(() -> HalfUpRounding.ofFractionalDecimalPlaces(-1, 0)); assertThrowsIllegalArg(() -> HalfUpRounding.ofFractionalDecimalPlaces(257, 0)); assertThrowsIllegalArg(() -> HalfUpRounding.ofFractionalDecimalPlaces(0, -1)); assertThrowsIllegalArg(() -> HalfUpRounding.ofFractionalDecimalPlaces(0, 257)); }
@DataProvider(name = "round") public static Object[][] data_round() { return new Object[][] { {HalfUpRounding.ofDecimalPlaces(2), 12.3449, 12.34}, {HalfUpRounding.ofDecimalPlaces(2), 12.3450, 12.35}, {HalfUpRounding.ofDecimalPlaces(2), 12.3451, 12.35}, {HalfUpRounding.ofDecimalPlaces(2), 12.3500, 12.35}, {HalfUpRounding.ofDecimalPlaces(2), 12.3549, 12.35}, {HalfUpRounding.ofDecimalPlaces(2), 12.3550, 12.36}, {HalfUpRounding.ofFractionalDecimalPlaces(2, 2), 12.3424, 12.340}, {HalfUpRounding.ofFractionalDecimalPlaces(2, 2), 12.3425, 12.345}, {HalfUpRounding.ofFractionalDecimalPlaces(2, 2), 12.3426, 12.345}, {HalfUpRounding.ofFractionalDecimalPlaces(2, 2), 12.3449, 12.345}, {HalfUpRounding.ofFractionalDecimalPlaces(2, 2), 12.3450, 12.345}, {HalfUpRounding.ofFractionalDecimalPlaces(2, 2), 12.3451, 12.345}, }; }
public void test_ofFractionalDecimalPlaces() { HalfUpRounding test = HalfUpRounding.ofFractionalDecimalPlaces(4, 32); assertEquals(test.getDecimalPlaces(), 4); assertEquals(test.getFraction(), 32); assertEquals(test.toString(), "Round to 1/32 of 4dp"); assertEquals(Rounding.ofFractionalDecimalPlaces(4, 32), test); }
public void coverage() { HalfUpRounding test = HalfUpRounding.ofDecimalPlaces(4); coverImmutableBean(test); HalfUpRounding test2 = HalfUpRounding.ofFractionalDecimalPlaces(4, 32); coverBeanEquals(test, test2); }