sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 04/07: Use of scientific notation needs to take in account the number of fraction digits that the NumberFormat allows.
Date Tue, 23 Jun 2020 17:57:42 GMT
This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit 88635fd67311a0a9068b89c288c98e6f7bf0fc7e
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Tue Jun 23 16:34:43 2020 +0200

    Use of scientific notation needs to take in account the number of fraction digits that
the NumberFormat allows.
---
 .../main/java/org/apache/sis/internal/util/Numerics.java  | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/core/sis-utility/src/main/java/org/apache/sis/internal/util/Numerics.java b/core/sis-utility/src/main/java/org/apache/sis/internal/util/Numerics.java
index 27edc5e..3f13f62 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/internal/util/Numerics.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/internal/util/Numerics.java
@@ -26,6 +26,7 @@ import org.apache.sis.util.Static;
 import org.apache.sis.util.Workaround;
 import org.apache.sis.util.ComparisonMode;
 import org.apache.sis.math.DecimalFunctions;
+import org.apache.sis.math.MathFunctions;
 import org.apache.sis.math.Statistics;
 
 import static java.lang.Math.min;
@@ -610,13 +611,19 @@ public final class Numerics extends Static {
      */
     @Workaround(library="JDK", version="10")
     public static String useScientificNotationIfNeeded(final Format format, final Object
value, final BiFunction<Format,Object,String> action) {
-        if (value instanceof Number) {
+        if (value instanceof Number && format instanceof DecimalFormat) {
+            final DecimalFormat df = (DecimalFormat) format;
+            final int maxFD = df.getMaximumFractionDigits();
             final double m = abs(((Number) value).doubleValue());
-            if (m > 0 && (m >= 1E+9 || m < 1E-4) && format instanceof
DecimalFormat) {
-                final DecimalFormat df = (DecimalFormat) format;
+            if (m > 0 && (m >= 1E+9 || m < MathFunctions.pow10(-Math.min(maxFD,
6)))) {
+                final int minFD = df.getMinimumFractionDigits();
                 final String pattern = df.toPattern();
-                df.applyPattern("0.######E00");
                 try {
+                    df.applyPattern("0.######E00");
+                    if (maxFD > 0) {
+                        df.setMinimumFractionDigits(minFD);
+                        df.setMaximumFractionDigits(maxFD);
+                    }
                     return action.apply(format, value);
                 } finally {
                     df.applyPattern(pattern);


Mime
View raw message