cancel
Showing results for 
Search instead for 
Did you mean: 

.toFixed(2) function behaves differently than Javascript version

gbreck
New Contributor

I have noticed that the expression language version of the toFixed function rounds up on “5” but the Javascript version in web coding does not. Is this intentional? I did some research and maybe the SnapLogic version is using the Java version: .setScale(2, BigDecimal.ROUND_HALF_UP) for example. toFixed-comparison

2 REPLIES 2

tstack
Former Employee

Your research is correct, we are using setScale() with ROUND_HALF_UP.

As to the rounding behavior, web browsers seem to round up on ‘5’ in some cases as well. If you try toFixed() on the value ‘2.35’, it will return 2.4 in Chrome and FireFox. I’m not sure what causes the difference at the moment, but it might be due to the differences between using floating point numbers and BigDecimals. The BigDecimals will be more accurate in their results since floating point is more of an approximation.

aleung
Contributor III

or you can use this in the mapper

Math.trunc(($[FIELD]++0.005)*100)/100