X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.g3d%2Fsrc%2Forg%2Fsimantics%2Fg3d%2Fmath%2FMathTools.java;h=b9941c8b94a774605ed91fd55752150fe3aab88d;hb=refs%2Fchanges%2F88%2F3688%2F2;hp=aa8b0551fdc845ac8db698724f6ad38d7d612b76;hpb=b0e0a3863a65fa5351d8a1207f94ca37135c675a;p=simantics%2F3d.git diff --git a/org.simantics.g3d/src/org/simantics/g3d/math/MathTools.java b/org.simantics.g3d/src/org/simantics/g3d/math/MathTools.java index aa8b0551..b9941c8b 100644 --- a/org.simantics.g3d/src/org/simantics/g3d/math/MathTools.java +++ b/org.simantics.g3d/src/org/simantics/g3d/math/MathTools.java @@ -1010,4 +1010,25 @@ public class MathTools { mat.m23 = -(f+n)/(f-n); return mat; } + + /** + * 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; + } + } }