Skip to content

Commit

Permalink
Make DecimalType recognize lowercased e notation
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Tanagra <[email protected]>
  • Loading branch information
jimtng committed Oct 7, 2023
1 parent 0d355fb commit b2d682b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public DecimalType(String value, Locale locale) {
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(locale);
df.setParseBigDecimal(true);
ParsePosition position = new ParsePosition(0);
BigDecimal parsedValue = (BigDecimal) df.parseObject(value, position);
BigDecimal parsedValue = (BigDecimal) df.parseObject(value.toUpperCase(locale), position);
if (parsedValue == null || position.getErrorIndex() != -1 || position.getIndex() < value.length()) {
throw new NumberFormatException("Invalid BigDecimal value: " + value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import java.text.DecimalFormatSymbols;
import java.util.IllegalFormatConversionException;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Stream;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.openhab.core.library.unit.Units;

/**
Expand Down Expand Up @@ -82,12 +82,36 @@ public void testKnownInvalidConstructors(Locale locale) {
assertThrows(NumberFormatException.class, () -> new DecimalType("123٬123٫56", Locale.GERMAN));
}

static Stream<Map.Entry<String, BigDecimal>> stringValues() {
return Stream.of( //
Map.entry("-2,000.5", new BigDecimal(-2000.5)), //
Map.entry("-2.5", new BigDecimal(-2.5)), //
Map.entry("-2", new BigDecimal(-2)), //
Map.entry("-0", new BigDecimal(0)), //
Map.entry("0", new BigDecimal(0)), //
Map.entry("2", new BigDecimal(2)), //
Map.entry("2.5", new BigDecimal(2.5)), //
Map.entry("2,000.5", new BigDecimal(2000.5)), //
Map.entry("-10E3", new BigDecimal("-1.0E4")), //
Map.entry("-10E-3", new BigDecimal("-0.010")), //
Map.entry("-0E-22", new BigDecimal("-0E-22")), //
Map.entry("-0E0", new BigDecimal(0)), //
Map.entry("-0E-0", new BigDecimal(0)), //
Map.entry("0E0", new BigDecimal(0)), //
Map.entry("0E-22", new BigDecimal("0E-22")), //
Map.entry("10E-3", new BigDecimal("0.010")), //
Map.entry("10E3", new BigDecimal("10E3")), //
Map.entry("1e3", new BigDecimal("1E3")), //
Map.entry("2.5e3", new BigDecimal("2.5E3")), //
Map.entry("-2.5e3", new BigDecimal("-2.5E3"))//
);
}

@ParameterizedTest
@ValueSource(strings = { "-2,000.5", "-2.5", "-2", "-0", "0", "2", "2.5", "2,000.5", "-10E3", "-10E-3", "-0E-22",
"-0E0", "-0E-0", "0E0", "0E-22", "10E-3", "10E3" })
public void testValidConstructors(String value) throws Exception {
new DecimalType(value);
DecimalType.valueOf(value);
@MethodSource("stringValues")
public void testValidConstructors(Map.Entry<String, BigDecimal> entry) throws Exception {
new DecimalType(entry.getKey());
assertEquals(DecimalType.valueOf(entry.getKey()).toBigDecimal(), entry.getValue());
}

@ParameterizedTest
Expand Down

0 comments on commit b2d682b

Please sign in to comment.