Skip to content

Commit

Permalink
Merge pull request #106 from Segelzwerg/Issue#105
Browse files Browse the repository at this point in the history
Solve LocationFormatter Bug
  • Loading branch information
jenetics authored Dec 15, 2019
2 parents cb540e0 + 65de9d4 commit ba07d16
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
22 changes: 11 additions & 11 deletions jpx/src/main/java/io/jenetics/jpx/format/LocationFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,9 @@
import static java.util.Objects.requireNonNull;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -516,7 +510,10 @@ public Builder append(
* @throws NullPointerException if one of the arguments is {@code null}
*/
public Builder append(final Location.Field field, final String pattern) {
return append(field, () -> new DecimalFormat(pattern));
return append(field, () -> {
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(Locale.US);
return new DecimalFormat(pattern, symbols);
});
}

/**
Expand Down Expand Up @@ -710,8 +707,11 @@ private void parsePattern(final String pattern) {
}
fmt.add(LocationFieldFormat.of(
field.get(),
() -> new DecimalFormat(
field.get().toDecimalPattern(token))
() -> {
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(Locale.US);
return new DecimalFormat(
field.get().toDecimalPattern(token), symbols);
}
));
} else {
fmt.add(ConstFormat.of(token));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@
import static io.jenetics.jpx.format.LocationFormatter.ISO_MEDIUM;
import static io.jenetics.jpx.format.LocationFormatter.ISO_SHORT;

import java.text.DecimalFormatSymbols;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import io.jenetics.jpx.format.Location.Field;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -190,4 +193,29 @@ public Object[][] patterns() {
.toArray(Object[][]::new);
}

@Test
public void testAppend() {
Locale.setDefault(Locale.GERMANY);
DecimalFormatSymbols instance = DecimalFormatSymbols.getInstance();
Location location = Location.of(Latitude.ofDegrees(23.987635));
LocationFormatter locationFormatter = LocationFormatter.builder()
.append(Field.DEGREE_OF_LATITUDE, "00.00")
.build();
Assert.assertEquals(instance.getDecimalSeparator(), ',');
Assert.assertEquals(locationFormatter.toPattern(), "DD.DD");
Assert.assertEquals(locationFormatter.format(location), "23.99");
}

@Test
public void testParsePattern() {
Locale.setDefault(Locale.GERMANY);
DecimalFormatSymbols instance = DecimalFormatSymbols.getInstance();
Location location = Location.of(Latitude.ofDegrees(23.987635));
LocationFormatter locationFormatter = LocationFormatter.builder()
.appendPattern("DD.DD")
.build();
Assert.assertEquals(instance.getDecimalSeparator(), ',');
Assert.assertEquals(locationFormatter.toPattern(), "DD.DD");
Assert.assertEquals(locationFormatter.format(location), "23.99");
}
}

0 comments on commit ba07d16

Please sign in to comment.