commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tommy Tynjä <>
Subject [beanutils] Precision lost when converting BigDecimal
Date Tue, 30 Sep 2014 07:26:00 GMT

I'm using commons-beanutils in my current project for bean copy operations.
I have a use case where I would like to convert a domain specific object
which extends BigDecimal to a regular java.math.BigDecimal. If my domain
specific object contains decimals, precision is lost at time of conversion.
A BigDecimal of e.g. value 3200.11 will be converted to a BigDecimal with
value 3200. This happens due to
org.apache.commons.beanutils.converters.NumberConverter does a
BigDecimal.valueOf(value.longValue()) at line 375 (current trunk). I've
created a reproducing test case which demonstrates the behavior (please see
the attached TestCase_ExtendingBigDecimal.patch file).

Is this a bug or how am I expected to handle this use case?

Adding the following code (please see attached NumberConverter.patch file)
before executing "else { return
targetType.cast(BigDecimal.valueOf(value.longValue())); }" in
NumberConverter seems to fix this issue (and all project test cases pass):
} else if (value instanceof BigDecimal) {
return targetType.cast(value);

Best regards
Tommy Tynjä

View raw message