Skip to content

Commit

Permalink
fix bigdecimal impl for strings that would otherwise be exponetials; …
Browse files Browse the repository at this point in the history
…paired with Richard Jensen
  • Loading branch information
paul-hammant committed Oct 28, 2010
1 parent 103a13b commit 13c0322
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public Object convertValue(String value, Type type) {
} else if (type == BigInteger.class) {
return BigInteger.valueOf(n.longValue());
} else if (type == BigDecimal.class) {
return new BigDecimal(ensureAllDigitsArePresent(n.doubleValue(), value));
return new BigDecimal(canonicalize(value));
} else {
return n;
}
Expand All @@ -167,20 +167,18 @@ public Object convertValue(String value, Type type) {
}
}

private static String ensureAllDigitsArePresent(double valueReducedToDouble, String valueAsString) {
String value = "" + valueReducedToDouble; // might have dropped trailing "0";
int i = -1;
for (char c : valueAsString.toCharArray()) {
if (Character.isDigit(c)) {
i = value.indexOf(c, i+1);
if (i == -1) {
value = value + c; // should be a "0" if missing
i = value.length();
}
}
private String canonicalize(String value) {
char dpSep = numberFormat.format(1.01).charAt(1);
int dpPosn = value.lastIndexOf(dpSep);
value = value.trim();
if (dpPosn != -1) {
String sf = value.substring(0, dpPosn).replace("[^0-9]", "");
String dp = value.substring(dpPosn+1);
return sf + "." + dp;
}
return value;
return value.replace(' ', ',');
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void shouldConvertValuesToNumbersWithLocalizedNumberFormat() {
assertThat((Long) enConverter.convertValue("100,000", Long.class), equalTo(100000L));
assertThat((Float) enConverter.convertValue("100,000.01", Float.class), equalTo(100000.01f));
assertThat((Double) enConverter.convertValue("100,000.01", Double.class), equalTo(100000.01d));
assertThat((Double) enConverter.convertValue("1,00,000.01", Double.class), equalTo(100000.01d)); //Hindi style
ParameterConverter frConverter = new NumberConverter(NumberFormat.getInstance(Locale.FRENCH));
assertThatAllNumberTypesAreAccepted(frConverter);
assertThatAllNumbersAreConverted(frConverter, Locale.FRENCH);
Expand Down Expand Up @@ -99,6 +100,7 @@ private void assertThatAllNumbersAreConverted(ParameterConverter converter, Loca
assertThat((BigInteger) converter.convertValue("3", BigInteger.class), equalTo(new BigInteger("3")));
assertThat((BigDecimal) converter.convertValue("3" + dot + "0", BigDecimal.class), equalTo(new BigDecimal("3.0")));
assertThat((BigDecimal) converter.convertValue("3" + dot + "00", BigDecimal.class), equalTo(new BigDecimal("3.00"))); // currency
assertThat((BigDecimal) converter.convertValue("30000000", BigDecimal.class), equalTo(new BigDecimal(30000000)));
assertThat((BigDecimal) converter.convertValue("3" + dot + "000", BigDecimal.class), equalTo(new BigDecimal("3.000"))); // something else!
assertThat((Number) converter.convertValue("3", Number.class), equalTo((Number)3L));
}
Expand Down

0 comments on commit 13c0322

Please sign in to comment.