Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OrdinateFormat to avoid NO locale bug #596

Merged
merged 1 commit into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,6 +36,8 @@
*/
public class OrdinateFormat
{
private static final String DECIMAL_PATTERN = "0";

/**
* The output representation of {@link Double#POSITIVE_INFINITY}
*/
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.locationtech.jts.io;

import java.util.Locale;

import junit.framework.TestCase;
import junit.textui.TestRunner;

Expand Down Expand Up @@ -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);
}
}