Handling BigDecimal in Talend
If you are dealing with money or precision is a must, use BigDecimal. Otherwise, Doubles tend to be good enough. Let's dive into this concept a little more.
Join the DZone community and get the full member experience.Join For Free
This post is very basic one since Talend is all about data integration. Finding a BigDecimal in such a dataset is very common.
BigDecimal vs. Doubles
A BigDecimal is an exact way of representing numbers. A Double has a certain precision. Working with Doubles of various magnitudes (say, d1=1000.0 and d2=0.001) could result in the 0.001 being dropped altogether when summing, as the difference in magnitude is so large. With BigDecimal, this would not happen.
The disadvantage of BigDecimal is that it's slower, and it's a bit more difficult to program algorithms that way (due to +, -, *, and / not being overloaded).
If you are dealing with money or precision is a must, use BigDecimal. Otherwise, Doubles tend to be good enough.
First, we go with a BigDecimal value such as 1.8772265500517E19. It means 1.8772265500517 x 1019 . We need to pass it without scientific notation. You can use a tJava comment in Talend and use simple Java to achieve this.
BigDecimal bigDecimal = new BigDecimal("1.8772265500517E19"); System.out.println(bigDecimal.toPlainString());
If you think you need to specify decimal point count, you can use the below line:
2 is the number of decimal places you want. You can change this as you need to.
Here is the output:
There are a few ways to achieve this such as Talend Routines, tJava, etc. But here, we used the tJava component. Add the below lines to the Basic setting panel tab:
double sampleDouble = 1.8772265500528E9; System.out.println(sampleDouble); NumberFormat formatter = new DecimalFormat("###.####"); String sampleDoubleString = formatter.format(sampleDouble); System.out.println(sampleDoubleString);
Then, add the below imports to the Advanced settings tab:
import java.text.NumberFormat; import java.text.DecimalFormat;
Here is the output of the job:
Make sure you used BigDecimal and Doubles in a correct way in the correct places.
Published at DZone with permission of Madhuka Udantha, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.