This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes JSP and the fly likes Dividing BigDecimals in Java or in JSTL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Dividing BigDecimals in Java or in JSTL" Watch "Dividing BigDecimals in Java or in JSTL" New topic
Author

Dividing BigDecimals in Java or in JSTL

caleb momanyi
Ranch Hand

Joined: Jun 17, 2012
Posts: 43

hello guys can somebody please help me understand why dividing two BigDecimals in JSTL throws


i have checked both values and i know they are both not equal to zero.

The only thing holding me back from throwing the computer out the window is the fact that i have solved the same problem before and it had something to do with setting round_mode but i can't remember how i did it.

in my jsp this is what i have


i am sure both totalA and totalB are not null or zero
Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19728
    
  10

Well, you can see from the stack trace that BigDecimal.divide is being executed. And you can read for yourself in the API documentation that an exception (division by zero) will be thrown when the denominator of the division is zero. So I suggest that the denominator of the division was zero and you have made an error when you determined it was not zero.
Gopakumar Naryanan
Ranch Hand

Joined: Jan 15, 2011
Posts: 72

caleb momanyi wrote:
i am sure both totalA and totalB are not null or zero


As per my knowledge, The Operator '/' in EL will not produce any Exception even though the denominator is '0'.. it produces 'infinity' as the result.
I tried to use opertaor '/' between two BigDecimal numbers in Java and got compilation Error :

operator / cannot be applied to java.math.BigDecimal,java.math.BigDecimal
System.out.println("Result : "+(b1 / b2));
^
1 error

Please provide your Code to make it more clear.


Thanks & Regards
Gopakumar
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Dividing BigDecimals in Java or in JSTL
 
It's not a secret anymore!