@@ -1159,6 +1159,18 @@ public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
11591159 * to convert.
11601160 */
11611161 static BigDecimal objectToBigDecimal (Object val , BigDecimal defaultValue ) {
1162+ return objectToBigDecimal (val , defaultValue , true );
1163+ }
1164+
1165+ /**
1166+ * @param val value to convert
1167+ * @param defaultValue default value to return is the conversion doesn't work or is null.
1168+ * @param exact When <code>true</code>, then {@link Double} and {@link Float} values will be converted exactly.
1169+ * When <code>false</code>, they will be converted to {@link String} values before converting to {@link BigDecimal}.
1170+ * @return BigDecimal conversion of the original value, or the defaultValue if unable
1171+ * to convert.
1172+ */
1173+ static BigDecimal objectToBigDecimal (Object val , BigDecimal defaultValue , boolean exact ) {
11621174 if (NULL .equals (val )) {
11631175 return defaultValue ;
11641176 }
@@ -1172,7 +1184,14 @@ static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue) {
11721184 if (!numberIsFinite ((Number )val )) {
11731185 return defaultValue ;
11741186 }
1175- return new BigDecimal (((Number ) val ).doubleValue ());
1187+ if (exact ) {
1188+ return new BigDecimal (((Number )val ).doubleValue ());
1189+ }else {
1190+ // use the string constructor so that we maintain "nice" values for doubles and floats
1191+ // the double constructor will translate doubles to "exact" values instead of the likely
1192+ // intended representation
1193+ return new BigDecimal (val .toString ());
1194+ }
11761195 }
11771196 if (val instanceof Long || val instanceof Integer
11781197 || val instanceof Short || val instanceof Byte ){
@@ -2132,8 +2151,8 @@ static boolean isNumberSimilar(Number l, Number r) {
21322151 // BigDecimal should be able to handle all of our number types that we support through
21332152 // documentation. Convert to BigDecimal first, then use the Compare method to
21342153 // decide equality.
2135- final BigDecimal lBigDecimal = objectToBigDecimal (l , null );
2136- final BigDecimal rBigDecimal = objectToBigDecimal (r , null );
2154+ final BigDecimal lBigDecimal = objectToBigDecimal (l , null , false );
2155+ final BigDecimal rBigDecimal = objectToBigDecimal (r , null , false );
21372156 if (lBigDecimal == null || rBigDecimal == null ) {
21382157 return false ;
21392158 }
0 commit comments