Four Stars

[resolved] String to Big-Decimal conversion

Hi All,
I have the situation where I need to convert the String result returned by an if-else block in Talend that I need to convert into bigdecimal. I have tried out the  below two options and none of them seem to be working
Option 1:
(BigDecimal) ( Col1="ABC" ? Col2 : Col3)
Option 2:
(new BigDecimal(Col1="ABC" ? Col2:Col3))
In this case both Col2 and Col3 are variables of string datatype in tmap.
Please share your suggestion on whether its possible to get this type of a conversion done.
1 ACCEPTED SOLUTION

Accepted Solutions
Fifteen Stars

Re: [resolved] String to Big-Decimal conversion

If field1 and field2 are BigDecimals, then you don't need to create a new one unless you are creating one for the "1" at the end.
The error you are getting is probably a nullpointerexception because you cannot use methods from a null object. You are trying to use ".toString()". If the object is null, you will get the nullpointerexception.
Why not use.....
row1.field1!=null ? row1.field1 : row1.field2!=null ? row1.field2 : new BigDecimal(1)
Rilhia Solutions
3 REPLIES
Fifteen Stars

Re: [resolved] String to Big-Decimal conversion

You are missing your row names from your "Col" names. You are also not checking for equality correctly. Assuming your row names are "row1", your code should look something like below...
new BigDecimal((row1.Col1.compareTo("ABC")==0 ? row1.Col2 : row1.Col3))
The in-line IF is contained inside brackets. This just aids readability. 
Rilhia Solutions
Four Stars

Re: [resolved] String to Big-Decimal conversion

I've tried using the expression below in the variable port of a tMap:
new BigDecimal((row1.field1.toString() != null ? row1.field1 Smiley Sadrow1.field2.toString() != null ? row1.field2 : 1)))
But I get Java exception errors while testing the expression. Please note that both field1 and field are of BigDecimal datatype and I need to check if they are null.
Fifteen Stars

Re: [resolved] String to Big-Decimal conversion

If field1 and field2 are BigDecimals, then you don't need to create a new one unless you are creating one for the "1" at the end.
The error you are getting is probably a nullpointerexception because you cannot use methods from a null object. You are trying to use ".toString()". If the object is null, you will get the nullpointerexception.
Why not use.....
row1.field1!=null ? row1.field1 : row1.field2!=null ? row1.field2 : new BigDecimal(1)
Rilhia Solutions