Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BigDecimal: divide round help

 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see how you got output with that code.

Could you post the code you used?
 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the value of tempProdQuant?
 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
15
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you're rounding 0.78/15 = 0.052 up to 0.1. The output sounds reasonable.
 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, Thank you very much for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic