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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Junilu Lacar
• Rob Spoor
• Paul Clapham
Saloon Keepers:
• Tim Holloway
• Tim Moores
• Jesse Silverman
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Piet Souris
• Frits Walraven

# BigDecimal divide question

Ranch Hand
Posts: 116
• Number of slices to send:
Optional 'thank-you' note:
Hello All,

Was getting the error - "java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result" while trying to do a divide of a BigDecimal by a BigDecimal. Found some code that I used to solve this problem above. The code I have does not look pretty. Wondering if there is a easier way to do the same.
Here's the code I have -

Here's the output -

Ranch Hand
Posts: 1296
• Number of slices to send:
Optional 'thank-you' note:
How many decimal places do you want to represent?

Tariq Ahsan
Ranch Hand
Posts: 116
• Number of slices to send:
Optional 'thank-you' note:
Thanks Garrett for your response. I would like to have 2 decimal places.

Tariq Ahsan
Ranch Hand
Posts: 116
• Number of slices to send:
Optional 'thank-you' note:
Garrett,

Just tried the changes below with your code to get 2 decimal position output. Is there any other simpler way?

The output :

Garrett Rowe
Ranch Hand
Posts: 1296
• Number of slices to send:
Optional 'thank-you' note:
This is a little simpler:

Tariq Ahsan
Ranch Hand
Posts: 116
• Number of slices to send:
Optional 'thank-you' note:
Garrett,

Worked perfectly!

Thanks a lot!

Tariq

Tariq Ahsan
Ranch Hand
Posts: 116
• Number of slices to send:
Optional 'thank-you' note:
Ugh...

The code did'nt quite work for me for the inputs as shown below -

Getting the output as - 78. But expect - 78.34

Any suggestions?

Thanks

Marshal
Posts: 74606
335
• Number of slices to send:
Optional 'thank-you' note:
Read the MathContext specification. The 2 appears to refer to significant figures, not places after the decimal point.

Tariq Ahsan
Ranch Hand
Posts: 116
• Number of slices to send:
Optional 'thank-you' note:
Any idea if I want to have result to be with 2 decimal points? Eventually I have to store the output value in NUMBER(9,2) datatype column in a database table.

Thanks

Campbell Ritchie
Marshal
Posts: 74606
335
• Number of slices to send:
Optional 'thank-you' note:
There are several overloaded divide methods. Have you been through them? Are they of any use? Note what "scale" means.

Tariq Ahsan
Ranch Hand
Posts: 116
• Number of slices to send:
Optional 'thank-you' note:
So far, the code below is working for me. But again need to try out exhaustively with all kinds of decimal value combinations.

Getting 78.34 as expected

Tariq Ahsan
Ranch Hand
Posts: 116
• Number of slices to send:
Optional 'thank-you' note:
Or better use -

Was getting 79.02 with CEILING. With HALF_EVEN getting the correct result - 79.01. For the num with 1.16 getting the expected 78.34.

 You showed up just in time for the waffles! And this tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton