如何将小数长度降低到空手道框架中最多两个小数位

发布于 2025-02-10 08:53:32 字数 264 浏览 2 评论 0原文

下面的功能是返回输出为8.1100000000001。我只想输出最多两个小数点。即)8.11

  • [2.16,1.34,1.32,1.25,0.65,0.48,48,0.42,42,0.26,0.26,0.14,06,0.06,03,0
  • def array =
  • ] '结果'); karate.set('result',temp + x)}
  • bacarate.foreach(数组,有趣)
  • 打印结果

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独自←快乐 2025-02-17 08:53:32

这是使用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]

但这可能有一些局限性。只需编写一个函数即可进行一次转换并使用它。请注意,您可以使用 java Interop 如果您想使用bigdecimal等。在网上找到大量解决方案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文