英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论