Custom transform - Math:round to set decimal places

193 views
Skip to first unread message

Leona Chen

unread,
Nov 4, 2021, 11:41:51 AM11/4/21
to CDAP User
Hi,
I am trying to use 'custom transform' function on a decimal field to set rounding decimal places.

one of my example value would be ' 3.24568690' and I would like to to change it to 3.2457. so I will input a forumar in custom transform = math:round(fieldname, 4).

However, this does not work and I got a jexl error message saying the code is invalid. 

need some help on what I did wrong. 

many thanks!!!

Greeshma Swaminathan

unread,
Nov 4, 2021, 12:31:51 PM11/4/21
to CDAP User
Hi Leona,

The 'math' API used in this case is java.lang.Math and the round method does not take a second argument. Try using DecimalFormat or BigDecimal depending on what datatype you are expecting.
Here is a a sample custom transform expression, this however returns a string type - `new("java.text.DecimalFormat","0.0000").format(col-name)`  . More details on JEXL syntax is available here.

Thanks,
Greeshma

Leona Chen

unread,
Nov 5, 2021, 6:48:07 AM11/5/21
to CDAP User
Hi Greeshma,

should i input your custom transform expression like this (See screenshot)? it did not work, maybe I did it wrong. 


screenshot.pdf

Greeshma Swaminathan

unread,
Nov 5, 2021, 4:31:14 PM11/5/21
to CDAP User
Hi Leona, 

The custom transform expression that I shared expects a Double field, the field you are working with in the screenshot is of string type. Convert the field to Double and give it a try.

Thanks,
Greeshma

Reply all
Reply to author
Forward
0 new messages