Talend Connect
Virtual Summit
And visit the Customer
& Community Lounge.
May 27-28, wherever you are.

Highlighted
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
Highlighted
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
Looks like you used BigDecimal. To calculate with BigDecimal you could use for example:
`rowN.yourBigDecimal.multiply(3.14);`

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
Highlighted
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
Highlighted
One Star

## Re: Help with BigDecimal/Double and writing Flat Files

Hi Sean,
take a look on the scale methods.
Bye
Volker
Highlighted
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.
Regards,
Sean

## OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

## Best Practices for Using Context Variables with Talend – Part 2

Part 2 of a series on Context Variables

Blog

## Best Practices for Using Context Variables with Talend – Part 1

Learn how to do cool things with Context Variables

Blog

## Migrate Data from one Database to another with one Job using the Dynamic Schema

Find out how to migrate from one database to another using the Dynamic schema

Blog