File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes BigDecimal: divide round help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "BigDecimal: divide round help" Watch "BigDecimal: divide round help" New topic
Author

BigDecimal: divide round help

Alessandro Ilardo
Ranch Hand

Joined: Dec 23, 2005
Posts: 218
Hello there,
I need an help using BigDecimal's API -
What I should get it'd always be an integer result up rounded.

So, if the division result is 0.12 or 1.99, it should be rounded up 1.00 or 2.00

So far, in a division like: new Double(0.78 .divide(15, 1, BigDecimal.ROUND_UP).doubleValue());
I get 0.1

How can I fix it?
Thanks in advance.
[ October 23, 2006: Message edited by: Alessandro Ilardo ]

trying to decode a woman mind....
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I don't see how you got output with that code.

Could you post the code you used?
Alessandro Ilardo
Ranch Hand

Joined: Dec 23, 2005
Posts: 218


from the logger:

2006-10-23 17:24:06,008 DEBUG [TP-Processor10] OrdersManager - TASK - addBasket(quantity): 0.78
2006-10-23 17:24:06,008 DEBUG [TP-Processor10] OrdersManager - TASK - addBasket(quantity /): 0.1
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
What is the value of tempProdQuant?
Alessandro Ilardo
Ranch Hand

Joined: Dec 23, 2005
Posts: 218
15
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
So you're rounding 0.78/15 = 0.052 up to 0.1. The output sounds reasonable.
Alessandro Ilardo
Ranch Hand

Joined: Dec 23, 2005
Posts: 218
Yes, but as result I need an Integer which is rounded away from 0

So, if the division result is 0.12 or 1.99, it should be rounded up 1.00 or 2.00
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I think you need to change the scale to 0 because that signifies how many digits there are after the decimal point.
Alessandro Ilardo
Ranch Hand

Joined: Dec 23, 2005
Posts: 218
You're right, Thank you very much for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BigDecimal: divide round help