1

Below function is returning the output is 8.110000000000001. I want the output only upto two decimal places. i.e) 8.11

  • def array = [2.16,1.34,1.32,1.25,0.65,0.48,0.42,0.26,0.14,0.06,0.03,0]
  • def result = 0
  • def fun = function(x){ var temp = karate.get('result'); karate.set('result', temp + x )}
  • karate.forEach(array, fun)
  • print result
Narasimha
  • 21
  • 3

1 Answers1

0

This is one way, using the JS Math.round().

* def response = [1.001, 1.011, 1.015]
* def rounded = response.map(x => x.toFixed(2) * 1)
* match rounded == [1.00, 1.01, 1.01]

But it may have some limitations. Just write a function to do the conversion once and use it. Note that you can use Java interop if you want to make use of BigDecimal etc. You can find plenty of solutions on the net.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248