From d7f8d75d06ca82a52cfda3bb59e17387017d0bf2 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 18 Sep 2017 21:24:22 +0200 Subject: [PATCH] Revert "Rounding issues visible in nightscout insulin field" --- .../java/info/nightscout/utils/Round.java | 38 +++++++------------ .../java/info/nightscout/utils/RoundTest.java | 3 +- 2 files changed, 14 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/info/nightscout/utils/Round.java b/app/src/main/java/info/nightscout/utils/Round.java index a45714ad45..26a4d5abce 100644 --- a/app/src/main/java/info/nightscout/utils/Round.java +++ b/app/src/main/java/info/nightscout/utils/Round.java @@ -1,37 +1,25 @@ package info.nightscout.utils; -import java.math.BigDecimal; - /** * Created by mike on 20.06.2016. */ public class Round { - public static Double roundTo(Double x, Double step) { - return round(x, step, BigDecimal.ROUND_HALF_UP); + public static Double roundTo(double x, Double step) { + if (x != 0d) { + return Math.round(x / step) * step; + } + return 0d; } - public static Double floorTo(Double x, Double step) { - return round(x, step, BigDecimal.ROUND_FLOOR); + if (x != 0d) { + return Math.floor(x / step) * step; + } + return 0d; } - public static Double ceilTo(Double x, Double step) { - return round(x, step, BigDecimal.ROUND_CEILING); - } - - private static Double round(Double x, Double step, int roundingMode) { - BigDecimal numberToRound = new BigDecimal((Double.toString(x))); - BigDecimal stepSize = new BigDecimal((Double.toString(step))); - int scale = getDecimalsFromStep(step); - - numberToRound.setScale(scale, BigDecimal.ROUND_HALF_UP); - - BigDecimal rounded = numberToRound.divide(stepSize, 0, roundingMode).multiply(stepSize); - - return rounded.doubleValue(); - } - - private static int getDecimalsFromStep(Double step) { - String stepString = Double.toString(step); - return stepString.substring(stepString.indexOf('.') + 1).length(); + if (x != 0d) { + return Math.ceil(x / step) * step; + } + return 0d; } } diff --git a/app/src/test/java/info/nightscout/utils/RoundTest.java b/app/src/test/java/info/nightscout/utils/RoundTest.java index 4ef1be054c..d95ab2b391 100644 --- a/app/src/test/java/info/nightscout/utils/RoundTest.java +++ b/app/src/test/java/info/nightscout/utils/RoundTest.java @@ -14,8 +14,6 @@ public class RoundTest { public void roundToTest() throws Exception { assertEquals( 0.55d, Round.roundTo(0.54d, 0.05d), 0.00000001d ); assertEquals( 1d, Round.roundTo(1.49d, 1d), 0.00000001d ); - assertEquals( 80d, Round.roundTo(65d, 40d), 0.00000001d ); - assertEquals( 0d, Round.roundTo(0d, 0.1d), 0.00000001d); } @Test @@ -29,4 +27,5 @@ public class RoundTest { assertEquals( 0.6d, Round.ceilTo(0.54d, 0.1d), 0.00000001d ); assertEquals( 2d, Round.ceilTo(1.49999d, 1d), 0.00000001d ); } + } \ No newline at end of file