
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
(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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Can you put a screenshot. Second solution should work for Java >= 1.5 (see attached).
2019-05-27 09_23_57-Talend Open Studio for Data Integration (7.1.1.20181026_1147) _ GettingStarted (...
2019-05-27 09_24_09-Talend Open Studio for Data Integration (7.1.1.20181026_1147) _ GettingStarted (...

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Here are the screenshot :
The formula :
Here is the error :
The error occured when I added the RoundingMode.
Tanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Tanks for help
