diff --git a/src/main/java/org/vaadin/addons/dramafinder/element/utils/NumberUtils.java b/src/main/java/org/vaadin/addons/dramafinder/element/utils/NumberUtils.java index ac26606..5562b9a 100644 --- a/src/main/java/org/vaadin/addons/dramafinder/element/utils/NumberUtils.java +++ b/src/main/java/org/vaadin/addons/dramafinder/element/utils/NumberUtils.java @@ -1,6 +1,8 @@ package org.vaadin.addons.dramafinder.element.utils; import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.util.Locale; public class NumberUtils { @@ -9,8 +11,11 @@ public static String formatDouble(double value) { if (value == Math.rint(value)) { return String.valueOf((long) value); } else { - // Use DecimalFormat to avoid scientific notation - DecimalFormat df = new DecimalFormat("0.######"); + // Use DecimalFormat to avoid scientific notation. Locale.ROOT keeps the + // decimal separator a "." regardless of the JVM default locale, matching + // what the browser reports for native input value/step/min/max. + DecimalFormat df = new DecimalFormat("0.######", + DecimalFormatSymbols.getInstance(Locale.ROOT)); return df.format(value); } } diff --git a/src/test/java/org/vaadin/addons/dramafinder/element/utils/NumberUtilsTest.java b/src/test/java/org/vaadin/addons/dramafinder/element/utils/NumberUtilsTest.java new file mode 100644 index 0000000..ac2b669 --- /dev/null +++ b/src/test/java/org/vaadin/addons/dramafinder/element/utils/NumberUtilsTest.java @@ -0,0 +1,55 @@ +package org.vaadin.addons.dramafinder.element.utils; + +import java.util.Locale; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class NumberUtilsTest { + + private final Locale defaultLocale = Locale.getDefault(); + private final Locale defaultFormatLocale = Locale + .getDefault(Locale.Category.FORMAT); + + @AfterEach + void restoreLocale() { + Locale.setDefault(defaultLocale); + Locale.setDefault(Locale.Category.FORMAT, defaultFormatLocale); + } + + @Test + void formatDouble_wholeValue_noFractionalPart() { + assertEquals("5", NumberUtils.formatDouble(5d)); + assertEquals("0", NumberUtils.formatDouble(0d)); + assertEquals("-3", NumberUtils.formatDouble(-3d)); + } + + @Test + void formatDouble_fractionalValue_dotSeparator() { + assertEquals("0.5", NumberUtils.formatDouble(0.5)); + assertEquals("5.5", NumberUtils.formatDouble(5.5)); + assertEquals("-1.25", NumberUtils.formatDouble(-1.25)); + } + + @Test + void formatDouble_commaSeparatorLocale_stillUsesDot() { + Locale.setDefault(Locale.FRANCE); + + assertEquals("0.5", NumberUtils.formatDouble(0.5)); + assertEquals("5.5", NumberUtils.formatDouble(5.5)); + } + + @Test + void formatDouble_commaSeparatorFormatCategory_stillUsesDot() { + Locale.setDefault(Locale.Category.FORMAT, Locale.GERMANY); + + assertEquals("0.5", NumberUtils.formatDouble(0.5)); + } + + @Test + void formatDouble_smallValue_noScientificNotation() { + assertEquals("0.000001", NumberUtils.formatDouble(0.000001)); + } +}