Post is very basic one, Since Talend is all about data integration. Finding a BigDecimal [1] in such data set 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 all together 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.

Big Decimal Sample

First we go with Big Decimal value such as ‘1.8772265500517E19’. It means 1.8772265500517 x 1019 . We need to pass it without scientific notation. You can used ‘tJava’ comment in Talend and used simple java to achieve this.

BigDecimal bigDecimal = new BigDecimal("1.8772265500517E19");
System.out.println(bigDecimal.toPlainString());

If you think you need to specific decimal point count also. Then you can used below line

System.out.printf("%1$.2f", bigDecimal);

2 is the number of decimal places you want. You can change as you need.

image

Here is the output

image

Doubles Sample

There are few ways to achieve this such as 'Talend Routines' or 'tJava', etc. But here we used tJava component. Add 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 below imports in to the ‘Advance Setting tab’

import java.text.NumberFormat;
import java.text.DecimalFormat;

image

Here is the out put of the job

image

Make sure you used BigDecimal and Doubles in correct way in correct places.

[1] https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html

0

Add a comment

Archives
Total Pageviews
Total Pageviews
2 0 5 7 7 0 6
Categories
Categories
Loading