Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

BigDecimal: divide round help

 
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 ]
 
Ranch Hand
Posts: 2410
  • 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: 2410
  • 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: 2410
  • 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: 2410
  • 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.
 
Their achilles heel is the noogie! Give them noogies tiny ad!
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic