This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository nuiton-utils. See http://git.nuiton.org/nuiton-utils.git commit 679c15ed23c1db809fdffea2546da88d06434f1f Author: Tony CHEMIT <chemit@codelutin.com> Date: Sat Jan 9 10:42:36 2016 +0100 Add more methods to NumberUtil (See #3861) --- src/main/java/org/nuiton/util/NumberUtil.java | 102 +++++++++++++++++++++- src/test/java/org/nuiton/util/NumberUtilTest.java | 49 +++++++++++ 2 files changed, 149 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/nuiton/util/NumberUtil.java b/src/main/java/org/nuiton/util/NumberUtil.java index 9ac9c55..aabc905 100644 --- a/src/main/java/org/nuiton/util/NumberUtil.java +++ b/src/main/java/org/nuiton/util/NumberUtil.java @@ -22,6 +22,12 @@ package org.nuiton.util; * #L% */ +import com.google.common.base.Predicate; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.math.BigDecimal; +import java.math.MathContext; import java.util.Arrays; /** @@ -31,13 +37,18 @@ import java.util.Arrays; public class NumberUtil { /** + * Logger. + */ + private static final Log log = LogFactory.getLog(NumberUtil.class); + + /** * Divide the divisor by the dividend. * Returns an array containing the quotients rounded up or down * to ensure the sum of the quotients equals the divisor. - * + * <p> * e.g. divideAndEnsureSum(100, 3) returns {34, 33, 33} * - * @param divisor the divisor + * @param divisor the divisor * @param dividend the dividend * @return an array whose length equals dividend */ @@ -61,4 +72,91 @@ public class NumberUtil { return result; } + + public static final Predicate<Integer> NULL_OR_ZERO_INTEGER = new Predicate<Integer>() { + + @Override + public boolean apply(Integer input) { + return input == null || input == 0; + } + }; + + public static final Predicate<Float> NULL_OR_ZERO_FLOAT_ONE_DIGIT = new Predicate<Float>() { + + @Override + public boolean apply(Float input) { + return input == null || Math.abs(roundOneDigit(input)) < 0.1; + } + }; + public static final Predicate<Float> NULL_OR_ZERO_FLOAT_TWO_DIGITS = new Predicate<Float>() { + + @Override + public boolean apply(Float input) { + return input == null || Math.abs(roundTwoDigits(input)) < 0.01; + } + }; + public static final Predicate<Float> NULL_OR_ZERO_FLOAT_THREE_DIGITS = new Predicate<Float>() { + + @Override + public boolean apply(Float input) { + return input == null || Math.abs(roundThreeDigits(input)) < 0.001; + } + }; + public static final Predicate<Float> NULL_OR_ZERO_FLOAT_FOUR_DIGITS = new Predicate<Float>() { + + @Override + public boolean apply(Float input) { + return input == null || Math.abs(roundFourDigits(input)) < 0.0001; + } + }; + + public static final Predicate<Float> NULL_OR_ZERO_FLOAT_FIVE_DIGITS = new Predicate<Float>() { + + @Override + public boolean apply(Float input) { + return input == null || Math.abs(roundFiveDigits(input)) < 0.00001; + } + }; + + protected static final MathContext mc1Digit = new MathContext(1); + protected static final MathContext mc2Digits = new MathContext(2); + protected static final MathContext mc3Digits = new MathContext(3); + protected static final MathContext mc4Digits = new MathContext(4); + protected static final MathContext mc5Digits = new MathContext(5); + + public static Float roundOneDigit(Float number) { + return round(number, mc1Digit); + } + + public static Float roundTwoDigits(Float number) { + return round(number, mc2Digits); + } + + public static Float roundThreeDigits(Float number) { + return round(number, mc3Digits); + } + + public static Float roundFourDigits(Float number) { + return round(number, mc4Digits); + } + + public static Float roundFiveDigits(Float number) { + return round(number, mc5Digits); + } + + public static Float roundNDigits(Float number, int digits) { + return round(number, new MathContext(digits)); + } + + public static Float round(Float number, MathContext mc) { + float old = number; + float partieEntier = (int) old; + float digit = old - (int) old; + number = partieEntier + new BigDecimal(digit).round(mc).floatValue(); + if (log.isDebugEnabled()) { + log.debug("round " + old + " to " + number + " with mc = " + mc); + } + return number; + } + } diff --git a/src/test/java/org/nuiton/util/NumberUtilTest.java b/src/test/java/org/nuiton/util/NumberUtilTest.java index 44bc78d..00d51d3 100644 --- a/src/test/java/org/nuiton/util/NumberUtilTest.java +++ b/src/test/java/org/nuiton/util/NumberUtilTest.java @@ -43,4 +43,53 @@ public class NumberUtilTest { Assert.assertArrayEquals(new int[]{ 17, 17, 17, 17, 16, 16 }, NumberUtil.divideAndEnsureSum(100, 6)); } + @Test + public void testRound3Digits() { + + assertRoundThreeDigits(1.2f, 1.2f); + assertRoundThreeDigits(1.22f, 1.22f); + assertRoundThreeDigits(1.222f, 1.222f); + assertRoundThreeDigits(1.2222f, 1.222f); + assertRoundThreeDigits(1.2225f, 1.222f); + assertRoundThreeDigits(1.2226f, 1.223f); + assertRoundThreeDigits(11.2226f, 11.223f); + assertRoundThreeDigits(111.2226f, 111.223f); + assertRoundThreeDigits(1111.2226f, 1111.223f); + + assertRoundThreeDigits(1111.999f, 1111.999f); + assertRoundThreeDigits(1111.9994f, 1111.999f); + assertRoundThreeDigits(1111.9995f, 1112f); + assertRoundThreeDigits(1111.9996f, 1112f); + } + + @Test + public void testRoundOneDigit() { + + assertRoundOneDigit(1.2f, 1.2f); + assertRoundOneDigit(1.22f, 1.2f); + assertRoundOneDigit(1.5f, 1.5f); + assertRoundOneDigit(1.55f, 1.5f); + assertRoundOneDigit(1.56f, 1.6f); + assertRoundOneDigit(1.9f, 1.9f); + assertRoundOneDigit(1.222f, 1.2f); + assertRoundOneDigit(11.2226f, 11.2f); + assertRoundOneDigit(111.2226f, 111.2f); + assertRoundOneDigit(1111.2226f, 1111.2f); + + assertRoundOneDigit(1111.999f, 1112.0f); + assertRoundOneDigit(1111.9994f, 1112.0f); + assertRoundOneDigit(1111.9995f, 1112.0f); + assertRoundOneDigit(1111.9996f, 1112.0f); + } + + protected void assertRoundThreeDigits(float number, float expected) { + float actual = NumberUtil.roundThreeDigits(number); + Assert.assertEquals("" + expected, "" + actual); + } + + protected void assertRoundOneDigit(float number, float expected) { + Float actual = NumberUtil.roundOneDigit(number); + Assert.assertEquals("" + expected, "" + actual); + } + } -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.