+
+ /**
+ * Round the number to a given number of decimals, if the rounded result contains at least three
+ * zero trailing decimal places within the rounded result.
+ */
+ public static double round(double value, int nsig) {
+ if (Math.abs(value) < NEAR_ZERO)
+ return 0.0;
+
+ int decimals = (int) Math.round(Math.log10(value));
+ int shift = nsig - decimals;
+ double multiplier = Math.pow(10.0, shift);
+ long roundedValue = Math.round(value * multiplier);
+ if (roundedValue % 1000 == 0) {
+ // Rounding results in at least three zeroes at the end
+ return roundedValue / multiplier;
+ } else {
+ // Number was not close to a shorter decimal representation, return it as is
+ return value;
+ }
+ }