Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Handling BigDecimal in Talend

DZone's Guide to

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.

· Big Data Zone ·
Free Resource

The open source HPCC Systems platform is a proven, easy to use solution for managing data at scale. Visit our Easy Guide to learn more about this completely free platform, test drive some code in the online Playground, and get started today.

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.

BigDecimal Example

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:

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

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

image

Here is the output:

image

Doubles Example

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;

image

Here is the output of the job:

image

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

Managing data at scale doesn’t have to be hard. Find out how the completely free, open source HPCC Systems platform makes it easier to update, easier to program, easier to integrate data, and easier to manage clusters. Download and get started today.

Topics:
big data ,data integration ,bigdecimal ,doubles ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}