两个数字在Karate特性文件中的乘法似乎不正确。

huangapple go评论85阅读模式
英文:

Multiplication of two numbers in Karate feature file seems to be not correct

问题

以下是要翻译的内容:

观察到在 karate 特性文件中两个数字的乘法被错误地计算了

当我尝试以下乘法时

  • 打印 38.30 * 1.05

我得到了 40.214999999999996

我希望结果是 40.215

我需要进行任何设置吗,或者有没有解决这个问题的方法?请给予建议。

英文:

It is observed that the multiplication of two numbers in karate feature file is getting evaluated incorrectly

when i try the below multiplication

* print  38.30 * 1.05

I am getting 40.214999999999996

I would expect the result to be 40.215

Is there any setting I need to do or is there any solution to this problem? Kindly advise.

答案1

得分: 1

Karate使用JS在幕后,这是JS已知的问题。团队通常也不在Karate中进行数学计算,因为它不是为此设计的。

尽管如此,您可以使用Java的BigDecimal类来获得您想要的结果。

* def BD = Java.type('java.math.BigDecimal')
* def a = new BD('38.30')
* def b = new BD('1.05')
* def result = a.multiply(b)
* match result == 40.215

我将其留给您来编写一个辅助工具,以使这更容易。最后,还请参考文档,了解其他情况下可能需要强制使用BigDecimal的情况:https://github.com/karatelabs/karate#large-numbers

英文:

Karate uses JS behind the scenes, and this is a known problem with JS. Teams also typically don't do math in Karate and it is not designed for that.

That said, you will get the results you want by using the Java BigDecimal class.

* def BD = Java.type('java.math.BigDecimal')
* def a = new BD('38.30')
* def b = new BD('1.05')
* def result = a.multiply(b)
* match result == 40.215

I leave it as an exercise to you to write a helper utility that will make this easier. Finally also refer to the docs on other cases where you may need to force the use of a BigDecimal: https://github.com/karatelabs/karate#large-numbers

huangapple
  • 本文由 发表于 2023年5月17日 23:17:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定