Bigdecimal divide causes not terminating decimal

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Bigdecimal divide causes not terminating decimal

Deepak Agarwal-2
InvoiceServices.java :

billingAmount =
billingAmount.subtract(amountAlreadyIncluded.divide(billingQuantity));

This sometimes causes non terminating decimal problem. Should we use
instead:

billingAmount = billingAmount.subtract(new
BigDecimal(amountAlreadyIncluded.doubleValue()/(billingQuantity.doubleValue())));

--
Thanks,
Deepak Agarwal,

Mobile: +91 9501190044
Reply | Threaded
Open this post in threaded view
|

Re: Bigdecimal divide causes not terminating decimal

pierre.gaudin
Hi,

Javadoc says :
    "if the exact quotient cannot be represented (because it has a non-terminating decimal expansion) an ArithmeticException is thrown."

You should use divide method with rouding parameter :
      divide(divisor, roundingMode)
  
Pierre
On 11/01/2013 11:30, Deepak Agarwal wrote:
InvoiceServices.java :

billingAmount =
billingAmount.subtract(amountAlreadyIncluded.divide(billingQuantity));

This sometimes causes non terminating decimal problem. Should we use
instead:

billingAmount = billingAmount.subtract(new
BigDecimal(amountAlreadyIncluded.doubleValue()/(billingQuantity.doubleValue())));


Reply | Threaded
Open this post in threaded view
|

Re: Bigdecimal divide causes not terminating decimal

Deepak Agarwal
Actually I was expecting that roundingMode will take place after the result
is observed by the 'divide' . I will check it however.

On Fri, Jan 11, 2013 at 4:13 PM, pierre.gaudin <[hidden email]>wrote:

> **
> Hi,
>
> Javadoc says :
>     "if the exact quotient cannot be represented (because it has a
> non-terminating decimal expansion) an ArithmeticException is thrown."
>
> You should use divide method with rouding parameter :
>       divide(divisor, roundingMode)
>
> Pierre
>
> On 11/01/2013 11:30, Deepak Agarwal wrote:
>
> InvoiceServices.java :
>
> billingAmount =
> billingAmount.subtract(amountAlreadyIncluded.divide(billingQuantity));
>
> This sometimes causes non terminating decimal problem. Should we use
> instead:
>
> billingAmount = billingAmount.subtract(new
> BigDecimal(amountAlreadyIncluded.doubleValue()/(billingQuantity.doubleValue())));
>
>
>
>


--
Thanks,
Deepak Agarwal,

Paxcel Technologies Pvt Ltd.
Hartron Complex, Sector 18, Gurgaon, India.
E-Mail: [hidden email]
Mobile: +91 9501190044
Reply | Threaded
Open this post in threaded view
|

Re: Bigdecimal divide causes not terminating decimal

Scott Gray-2
In reply to this post by Deepak Agarwal-2
Doubles have no place in financial transactions, the correct fix is to set a scale on the divide operation.

Regards
Scott

On 11/01/2013, at 11:30 PM, Deepak Agarwal <[hidden email]> wrote:

> InvoiceServices.java :
>
> billingAmount =
> billingAmount.subtract(amountAlreadyIncluded.divide(billingQuantity));
>
> This sometimes causes non terminating decimal problem. Should we use
> instead:
>
> billingAmount = billingAmount.subtract(new
> BigDecimal(amountAlreadyIncluded.doubleValue()/(billingQuantity.doubleValue())));
>
> --
> Thanks,
> Deepak Agarwal,
>
> Mobile: +91 9501190044