Skip to content

Commit b0ffa23

Browse files
committed
Fix DecimalFormat use in non-english locales.
Refs #420.
1 parent 1b34dd6 commit b0ffa23

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/view/postanalysis/PAWeightPanel.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.awt.GridBagLayout;
1313
import java.awt.event.ActionListener;
1414
import java.text.DecimalFormat;
15+
import java.text.ParseException;
1516
import java.util.ArrayList;
1617
import java.util.Arrays;
1718
import java.util.Collections;
@@ -67,6 +68,9 @@ public class PAWeightPanel extends JPanel {
6768

6869
private static final double HYPERGOM_DEFAULT = 0.25;
6970

71+
private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat();
72+
73+
7074
@Inject private IconManager iconManager;
7175

7276
private final EnrichmentMap map;
@@ -212,19 +216,18 @@ private JPanel createRankTestSelectPanel() {
212216
JLabel cuttofLabel = new JLabel(LABEL_CUTOFF);
213217
JLabel dataSetLabel = new JLabel("Data Set:");
214218

215-
DecimalFormat decFormat = new DecimalFormat();
216-
decFormat.setParseIntegerOnly(false);
217-
rankTestTextField = new JFormattedTextField(decFormat);
219+
DECIMAL_FORMAT.setParseIntegerOnly(false);
220+
rankTestTextField = new JFormattedTextField(DECIMAL_FORMAT);
218221
rankTestTextField.setColumns(6);
219222
rankTestTextField.setHorizontalAlignment(JTextField.RIGHT);
220223
rankTestTextField.addPropertyChangeListener("value", e -> {
221224
String text = rankTestTextField.getText();
222225
try {
223-
double val = Double.parseDouble(text);
226+
double val = DECIMAL_FORMAT.parse(text).doubleValue();
224227
PostAnalysisFilterType filterType = getFilterType();
225228
savedFilterValues.put(filterType, val);
226229
showWarning(filterType.isValid(val) ? null : filterType.getErrorMessage());
227-
} catch(NumberFormatException ex) {
230+
} catch(ParseException ex) {
228231
showWarning("Not a number");
229232
}
230233

@@ -648,7 +651,13 @@ public FilterMetricSet getResults() {
648651

649652
public FilterMetric createFilterMetric(String datasetName) {
650653
String text = rankTestTextField.getText();
651-
double value = Double.parseDouble(text);
654+
double value;
655+
try {
656+
value = DECIMAL_FORMAT.parse(text).doubleValue();
657+
} catch (ParseException e) {
658+
return null;
659+
}
660+
652661
PostAnalysisFilterType type = getFilterType();
653662

654663
switch(type) {

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/view/util/SliderBarPanel.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
import java.util.Iterator;
6565
import java.util.List;
6666
import java.util.Optional;
67-
import java.util.stream.IntStream;
6867

6968
import javax.swing.GroupLayout;
7069
import javax.swing.GroupLayout.Alignment;
@@ -392,24 +391,21 @@ private void textFieldValueChanged() {
392391
private static String getFormatPattern(double... numbers) {
393392
StringBuilder sb = new StringBuilder("0.0"); // At least one decimal.
394393
int p = precision(numbers);
395-
396-
if (p > 0)
397-
IntStream.range(0, p).forEach(v -> sb.append("#")); // Optional decimal digits
398-
394+
for (int i = 0; i < p; i++) {
395+
sb.append('#');
396+
}
399397
return sb.toString();
400398
}
401399

402400
private static int precision(double... numbers) {
403-
DecimalFormat df = new DecimalFormat("0.##############################");
404401
int p = 0;
405-
406402
for (double n : numbers) {
407-
String text = df.format(n);
408-
409-
if (text.indexOf('.') >= 0)
410-
p = Math.max(p, text.substring(text.indexOf('.')).length() - 1);
403+
String text = Double.toString(n);
404+
int i = text.indexOf('.');
405+
if (i >= 0) {
406+
p = Math.max(p, text.substring(i).length() - 1);
407+
}
411408
}
412-
413409
return p;
414410
}
415411
}

0 commit comments

Comments
 (0)