Skip to main content
Announcements
A fresh, new look for the Data Integration & Quality forums and navigation! Read more about what's changed.
cancel
Showing results for 
Search instead for 
Did you mean: 
pawe84
Creator

Issue can't divide BigDecimal by BigDecimal in tMap

Hi everyone

I have an issue and can't figure out the problem Talend has


First I tried to except division by zero and the next line is just divide column1 by column2

row1.Sales.equals(0)|| row1.Purchase.equals(0) ? new BigDecimal(0):
row1.Sales.divide(row1.Purchase, 6, BigDecimal.ROUND_CEILING)

But I get this message

 

java.lang.ArithmeticException: / by zero
at java.math.BigDecimal.divideAndRound(Unknown Source)
at java.math.BigDecimal.divide(Unknown Source)
at java.math.BigDecimal.divide(Unknown Source)

 

What does it mean "unknown source"?

Thanks for any hints.

Labels (2)
1 Solution

Accepted Solutions
lennelei
Creator III

Hello,

 

Could you try this :

BigDecimal.ZERO.compareTo(row1.Purchase)==0?new BigDecimal(0):
row1.Sales.divide(row1.Purchase, 6, BigDecimal.ROUND_CEILING)

View solution in original post

5 Replies
lennelei
Creator III

Hello,

 

Could you try this :

BigDecimal.ZERO.compareTo(row1.Purchase)==0?new BigDecimal(0):
row1.Sales.divide(row1.Purchase, 6, BigDecimal.ROUND_CEILING)
pawe84
Creator
Author

Still get the same error.

lennelei
Creator III

Are you sure the error comes from this portion of code?

 

pawe84
Creator
Author

Thanks now it works, maybe I did a mistake in my code.

lennelei
Creator III

Good news, I should have explain more in details.

 

Technically speaking, with this code:

row1.Purchase.equals(0) 

You are trying to compare a BigDecimal with 0.

.equals() returns "true if and only if the specified Object is a BigDecimal whose value and scale are equal to this BigDecimal's." which is clearly not the case here!

 

Moreover, equals will compare two BigDecimal on value and scale, which means that even this would fail: 

BigDecimal bd1=new BigDecimal("0");
BigDecimal bd2=new BigDecimal("0.0");
bd1.equals(bd2) // will return false

You should use compareTo() in order to compare the value only.