AndroidAPS/app/src/main/java/info/nightscout/androidaps/utils/Round.java

35 lines
860 B
Java
Raw Normal View History

2019-02-26 20:38:27 +01:00
package info.nightscout.androidaps.utils;
2016-06-20 12:03:05 +02:00
import java.math.BigDecimal;
2016-06-20 12:03:05 +02:00
/**
* Created by mike on 20.06.2016.
*/
public class Round {
2017-06-15 22:55:07 +02:00
public static Double roundTo(double x, Double step) {
if (x == 0d) {
return 0d;
2016-06-20 12:03:05 +02:00
}
//Double oldCalc = Math.round(x / step) * step;
Double newCalc = BigDecimal.valueOf(Math.round(x / step)).multiply(BigDecimal.valueOf(step)).doubleValue();
// just for the tests, forcing failures
//newCalc = oldCalc;
return newCalc;
2016-06-20 12:03:05 +02:00
}
2016-06-20 20:45:55 +02:00
public static Double floorTo(Double x, Double step) {
if (x != 0d) {
return Math.floor(x / step) * step;
}
return 0d;
}
public static Double ceilTo(Double x, Double step) {
if (x != 0d) {
return Math.ceil(x / step) * step;
}
return 0d;
}
2016-06-20 12:03:05 +02:00
}