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

Hortonworks Sandbox for HDP and HDF is your chance to get started on learning, developing, testing and trying out new features. Each download comes preconfigured with interactive tutorials, sample data and developments from the Apache community.

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.

Hortonworks Community Connection (HCC) is an online collaboration destination for developers, DevOps, customers and partners to get answers to questions, collaborate on technical articles and share code examples from GitHub.  Join the discussion.

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 }}