You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Tommy Tynjä <to...@diabol.se> on 2014/09/30 09:26:00 UTC

[beanutils] Precision lost when converting BigDecimal

Hi.

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ä

Re: [beanutils] Precision lost when converting BigDecimal

Posted by Tommy Tynjä <to...@diabol.se>.
Hi.

Thank you for your feedback.

I've filed this issue as BEANUTILS-470 in JIRA.

Best regards,
Tommy Tynjä


> Hi,
>
> Am 30.09.2014 um 09:26 schrieb Tommy Tynjä:
> > Hi.
> >
> > 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);
> > }
>
> this is probably a bug. The best way to go is opening a new ticket in
> Jira [1] describing the problem. Please attach your unit test and your
> proposed patch.
>
> Thanks!
> Oliver
>
> >
> > Best regards
> > Tommy Tynjä
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> > For additional commands, e-mail: user-help@commons.apache.org
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>
>

Re: [beanutils] Precision lost when converting BigDecimal

Posted by Oliver Heger <ol...@oliver-heger.de>.
Hi,

Am 30.09.2014 um 09:26 schrieb Tommy Tynjä:
> Hi.
> 
> 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);
> }

this is probably a bug. The best way to go is opening a new ticket in
Jira [1] describing the problem. Please attach your unit test and your
proposed patch.

Thanks!
Oliver

> 
> Best regards
> Tommy Tynjä
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org