Help with BigDecimal/Double and writing Flat Files

One Star

Help with BigDecimal/Double and writing Flat Files

Hello!
I'll appreciate some help from the forum members.
I am writing a simple routine that is reading from a CSV file that has a bunch of values such as sales, promotion $ etc. I've defined all of these as BigDecimal (12,2). I am new to Java but this is the type I read one should define currency values with. Then in a TMAP, I am doing a simple calculation such as Sales * 3.14. This gives me an error saying that "*" is incompatible with BigDecimal. Why is that? What is a work around?
I tried this with DOUBLE and it works but sometimes it gives me values such as 8.1200000001 in the flat file even though the output mask is DOUBLE(12,2). I expected it to be rounded or truncated at 2 decimal places. This probably is a simple question but I was not able to find a ROUND function that worked with BIG DECIMAL or DOUBLE and will round down to 2 decimal places.
I am used to Informatica which kind of shields me from the internals and has plentiful functions. In this case, I would have defined the output file as DECIMAL (12,2) and used High Precision to get my results or used the ROUND(Sales, 2). Note that I use Informatica at my job but I am a fan of Talend. Though I tend to compare these 2 quite a bit when it comes to usability etc.
Any insights are welcome. I am aware that I need to get familiar with Java :-)
Thanks.
Sean
One Star

Re: Help with BigDecimal/Double and writing Flat Files

Hi Sean,
calculating with Double or double is not a good idea if you work with money Smiley Wink
Looks like you used BigDecimal. To calculate with BigDecimal you could use for example:
rowN.yourBigDecimal.multiply(3.14);

You can find more information in the Java API BigDecimal.
Good practice in Java is to multiply your value with the number of internal decimal places and use an integer.
And you are right, the drawback of Talend's flexibility is that you have more knowledge about java (or perl).
Bye
Volker
One Star

Re: Help with BigDecimal/Double and writing Flat Files

Thank you! This is helping me. I had to change it a bit for it to work.
row2.TKT_SALE_AMT.multiply(BigDecimal.valueOf(3.14))

This is one step closer but not quite there for me. Here for a value of 4.47, I get output as 14.0358 which is correct but I need to round it to 2 decimals only (this will be 14.04). I looked at the MathContext but all the methods round the input to an integer (http://java.sun.com/j2se/1.5.0/docs/api/java/math/RoundingMode.html#HALF_EVEN).
If possible, let me know what I am missing here agian.
Thanks
Sean
One Star

Re: Help with BigDecimal/Double and writing Flat Files

Hi Sean,
take a look on the scale methods.
Bye
Volker
One Star

Re: Help with BigDecimal/Double and writing Flat Files

It took me some time to get it to work but I finally did it using the setscale method.
Thank you for your help.
Regards,
Sean