From efe2a3a823700c2fb7630d14a2b33117531c3ff9 Mon Sep 17 00:00:00 2001 From: Martin Davis Date: Mon, 14 Sep 2020 16:38:22 -0700 Subject: [PATCH] Fix OrdinateFormat to avoid NO locale bug Signed-off-by: Martin Davis --- .../locationtech/jts/io/OrdinateFormat.java | 21 ++++++++++++++----- .../jts/io/OrdinateFormatTest.java | 14 +++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/modules/core/src/main/java/org/locationtech/jts/io/OrdinateFormat.java b/modules/core/src/main/java/org/locationtech/jts/io/OrdinateFormat.java index 5681151b27..3c5c1b8899 100644 --- a/modules/core/src/main/java/org/locationtech/jts/io/OrdinateFormat.java +++ b/modules/core/src/main/java/org/locationtech/jts/io/OrdinateFormat.java @@ -14,6 +14,8 @@ import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; +import java.text.NumberFormat; +import java.util.Locale; /** * Formats numeric values for ordinates @@ -34,6 +36,8 @@ */ public class OrdinateFormat { + private static final String DECIMAL_PATTERN = "0"; + /** * The output representation of {@link Double#POSITIVE_INFINITY} */ @@ -91,14 +95,21 @@ public OrdinateFormat(int maximumFractionDigits) { } private static DecimalFormat createFormat(int maximumFractionDigits) { - // specify decimal separator explicitly to work in all locales - DecimalFormatSymbols symbols = new DecimalFormatSymbols(); - symbols.setDecimalSeparator('.'); - DecimalFormat format = new DecimalFormat("0", symbols); + // ensure format uses standard WKY number format + NumberFormat nf = NumberFormat.getInstance(Locale.US); + // This is expected to succeed for Locale.US + DecimalFormat format; + try { + format = (DecimalFormat) nf; + } + catch (ClassCastException ex) { + throw new RuntimeException("Unable to create DecimalFormat for Locale.US"); + } + format.applyPattern(DECIMAL_PATTERN); format.setMaximumFractionDigits(maximumFractionDigits); return format; } - + /** * Returns a string representation of the given ordinate numeric value. * diff --git a/modules/core/src/test/java/org/locationtech/jts/io/OrdinateFormatTest.java b/modules/core/src/test/java/org/locationtech/jts/io/OrdinateFormatTest.java index 060e76e798..ed811cfef4 100644 --- a/modules/core/src/test/java/org/locationtech/jts/io/OrdinateFormatTest.java +++ b/modules/core/src/test/java/org/locationtech/jts/io/OrdinateFormatTest.java @@ -1,5 +1,7 @@ package org.locationtech.jts.io; +import java.util.Locale; + import junit.framework.TestCase; import junit.textui.TestRunner; @@ -70,4 +72,16 @@ private void checkFormat(double d, int maxFractionDigits, String expected) { String actual = format.format(d); assertEquals(expected, actual); } + + private void checkFormatAllLocales(double d, int maxFractionDigits, String expected) { + OrdinateFormat format = OrdinateFormat.create(maxFractionDigits); + String actual = format.format(d); + assertEquals(expected, actual); + } + + private void checkFormatLocales(Locale locale, double d, int maxFractionDigits, String expected) { + OrdinateFormat format = OrdinateFormat.create(maxFractionDigits); + String actual = format.format(d); + assertEquals(expected, actual); + } }