Skip to main content
Announcements
A fresh, new look for the Data Integration & Quality forums and navigation! Read more about what's changed.
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

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

Labels (1)
1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

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

8 Replies
Anonymous
Not applicable
Author

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.
Anonymous
Not applicable
Author

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.
Anonymous
Not applicable
Author

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.
Anonymous
Not applicable
Author

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

 

Anonymous
Not applicable
Author

Anonymous
Not applicable
Author

 

Here are the screenshot :

The formula : 

0683p000009M5Ba.png

Here is the error : 

0683p000009M5Bf.png

The error occured when I added the RoundingMode.

 

Tanks

Anonymous
Not applicable
Author

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

Anonymous
Not applicable
Author

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

0683p000009M4o3.png

Tanks for help