Tmap BigDecimal Divide operation

Six Stars

Tmap BigDecimal Divide operation

Hello, I have a problem with a divide operation in tmap component.

I would like a result with 2 decimal and I always obtain a number without decimal.

Here si my formula : 

(row1.T8_PRIXV6 == null ? new BigDecimal("0") : row1.T8_PRIXV6).divide(new BigDecimal("1.2"),2)

The source and the destination are BigDecimal.

Tanks for help


Accepted Solutions
Six Stars

Re: Tmap BigDecimal Divide operation

That's normal, you are using a rounding mode, not a scaling choice.

Re-read the API, use this instead :

 

public BigDecimal divide(BigDecimal divisor,
                int scale,
                RoundingMode roundingMode)

Returns a BigDecimal whose value is (this / divisor), and whose scale is as specified. If rounding must be performed to generate a result with the specified scale, the specified rounding mode is applied.

Parameters:
    divisor - value by which this BigDecimal is to be divided.
    scale - scale of the BigDecimal quotient to be returned.
    roundingMode - rounding mode to apply.

and not

 

public BigDecimal divide(BigDecimal divisor,
                int roundingMode)

Returns a BigDecimal whose value is (this / divisor), and whose scale is this.scale(). If rounding must be performed to generate a result with the given scale, the specified rounding mode is applied.

The new divide(BigDecimal, RoundingMode) method should be used in preference to this legacy method.

Parameters:
    divisor - value by which this BigDecimal is to be divided.
    roundingMode - rounding mode to apply.

View solution in original post


All Replies
Six Stars

Re: Tmap BigDecimal Divide operation

Form Java API, second parameter is rounding mode, not scale, scale is same a "this" (0, if null for your example).

 

public BigDecimal divide(BigDecimal divisor,
                int roundingMode)

Returns a BigDecimal whose value is (this / divisor), and whose scale is this.scale(). If rounding must be performed to generate a result with the given scale, the specified rounding mode is applied.

The new divide(BigDecimal, RoundingMode) method should be used in preference to this legacy method.

Parameters:
    divisor - value by which this BigDecimal is to be divided.
    roundingMode - rounding mode to apply.
Six Stars

Re: Tmap BigDecimal Divide operation

I tried to add the rounding mode :

(row1.T8_PRIXV6 == null ? new BigDecimal("0") : row1.T8_PRIXV6).divide(new BigDecimal("1.2"),3, BigDecimal.ROUND_CEILING)

But the result is still the same.
Six Stars

Re: Tmap BigDecimal Divide operation

That's normal, you are using a rounding mode, not a scaling choice.

Re-read the API, use this instead :

 

public BigDecimal divide(BigDecimal divisor,
                int scale,
                RoundingMode roundingMode)

Returns a BigDecimal whose value is (this / divisor), and whose scale is as specified. If rounding must be performed to generate a result with the specified scale, the specified rounding mode is applied.

Parameters:
    divisor - value by which this BigDecimal is to be divided.
    scale - scale of the BigDecimal quotient to be returned.
    roundingMode - rounding mode to apply.

and not

 

public BigDecimal divide(BigDecimal divisor,
                int roundingMode)

Returns a BigDecimal whose value is (this / divisor), and whose scale is this.scale(). If rounding must be performed to generate a result with the given scale, the specified rounding mode is applied.

The new divide(BigDecimal, RoundingMode) method should be used in preference to this legacy method.

Parameters:
    divisor - value by which this BigDecimal is to be divided.
    roundingMode - rounding mode to apply.

View solution in original post

Six Stars

Re: Tmap BigDecimal Divide operation

Here is the new Fomula I tried : 

 

(row1.T8_PRIXV6 == null ? new BigDecimal("0") : row1.T8_PRIXV6).divide(new BigDecimal("1.2"),3, RoundingMode.CEILING)

 

I also tried : 

(row1.T8_PRIXV6 == null ? new BigDecimal("0") : row1.T8_PRIXV6).divide(new BigDecimal("1.2"),3, java.math.RoundingMode.CEILING)

 

I get an error : RoundingMode cannot be resolve to a variable.

 

Tanks for help

 

Six Stars
Six Stars

Re: Tmap BigDecimal Divide operation

 

Here are the screenshot :

The formula : 

formule.png

Here is the error : 

erreur.png

The error occured when I added the RoundingMode.

 

Tanks

Six Stars

Re: Tmap BigDecimal Divide operation

I also tried the java component, here si the result : 

 

DÈmarrage du job Testjava a 22:10 27/05/2019.
[statistics] connecting to socket on port 3864
[statistics] connected
#### => 37.500
[statistics] disconnected

Job Testjava terminÈ ‡ 22:10 27/05/2019. [Code sortie=0]

 

The result is correct

Six Stars

Re: Tmap BigDecimal Divide operation

I found what was wrong in addition to your solution with the correct formula.

In the tmap component I add the precision to 2 digits, so now it works

solution.png

Tanks for help

2019 GARTNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

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

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog