如何对以字符串形式传入的值与双精度或浮点数值进行断言相等。

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

How to assertEquals on a value that is passed in as a String against a value which is a double or float

问题

以下是翻译好的内容:

我的方法如下:

public void variable_equal_to_ratio_of_and(String exp, String numerator, String denominator) {
    double numerTotal = 0, denomTotal = 0;
    String[] numerArray = numerator.split(",");
    String[] denomArray = denominator.split(",");
    System.out.println("PCT value " + exp);

    for (String iterator : numerArray) {
        numerTotal += Double.valueOf(iterator);
    }

    for (String iterator : denomArray) {
        denomTotal += Double.valueOf(iterator);
    }

    double ratio = (numerTotal * 100) / (numerTotal + denomTotal);

    ratio = ratio / 100;

    BigDecimal bd = new BigDecimal(ratio).setScale(3, RoundingMode.HALF_DOWN);
    double d = bd.doubleValue();

    org.junit.Assert.assertEquals(Float.valueOf(exp), d, 0);
}

为了确定我们的客户所看到的实际比例值,我需要进行一些数学运算,以获得答案并将该答案存储在double或float中,这就是您在此方法中看到的内容。问题是,我必须对测试框架从API中获取的内容进行断言比较,但是没有有效的方法可以将float/double与字符串进行比较以供junit.Assert使用。我似乎可以将传入的字符串转换为float/double,但预期值和实际值的小数位数必须相同,否则断言将失败。

当我将预期值转换为两位小数时,似乎总是使用DecimalFormat或BigDecimal完成的,而我都不能将它们传递给断言。

我可以将预期值字符串转换为两位小数,但是当我将其作为double/float传递给AssertEquals时,它会解析为任意数量的小数位数。

开发人员在GoLang中编写了以下比例代码:Math.round(((123.54999) * 10) / 10); 并告诉我它使用了以下公式:“float32((float64(float32(number1) * 100 / float32(number2))) / 100)”。

但是,我的QA负责人在Java中编写了我们整个测试框架。

如何将传递给断言的预期值保持与我在BigDecimal中设置的实际值的相同小数位数?

英文:

My method is as follows:

	public void variable_equal_to_ratio_of_and(String exp, String numerator, String denominator) {
	double numerTotal = 0, denomTotal = 0;
	String[] numerArray = numerator.split(",");
	String[] denomArray = denominator.split(",");
	System.out.println("PCT value " + exp);

	for (String iterator: numerArray) {
		numerTotal += Double.valueOf(iterator);
	}
	
	for (String iterator: denomArray) {
		denomTotal += Double.valueOf(iterator);
	}
	
	double ratio = (numerTotal * 100) / (numerTotal + denomTotal);

	ratio = ratio / 100;

	BigDecimal bd = new BigDecimal(ratio).setScale(3, RoundingMode.HALF_DOWN);
	double d = bd.doubleValue();

	org.junit.Assert.assertEquals(Float.valueOf(exp), d, 0);

}

To figure out what the actual value should be for this ratio our customers view, I need to do some math to get the answer and store that answer in a double or float which is what you're seeing in this method. The problem is I then have to do an assert equals on what our test framework pulls from the API and there is no valid method to compare a float/double with a string for junit.Assert. I seem to be able to convert the String passed in to a float/double, but then both the expected value and actual value has to be the same number of decimal places or the assertion fails.

When I convert the expected value to two decimals, it seems to always be done by DecimalFormat or BigDecimal and neither of those I can pass into the assertion.

I can convert the expected value string to 2 decimals, but then when I pass it into the AssertEquals as a double/float, it parses it to as many decimals as it wants.

The developers coded the ratio in GoLang as Math.round(((123.54999) * 10) / 10); and tell me it's using this formula: "float32((float64(float32(number1) * 100 / float32(number2))) / 100)"

But, my QA lead has written our entire testing framework in Java.

How can I keep the expected value passed to the assertion as the same number of decimals that I've set the actual value to in BigDecimal?

答案1

得分: 2

为了断言两个浮点数值相等到某个小数位数,你可以在assertEquals(第三个值)上更改epsilon参数。

例如,

Assert.assertEquals(Float.valueOf(exp), d, .01);

会断言Float.valueOf(exp)d在误差为.01的情况下是相等的。

英文:

To assert that 2 floating-point values are equal to some number of decimal places, you can change the epsilon parameter on assertEquals (the 3rd value)

For example,

Assert.assertEquals(Float.valueOf(exp), d, .01);

would assert that Float.valueOf(exp) and d are equivalent with an epsilon of .01.

huangapple
  • 本文由 发表于 2020年7月25日 02:29:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63079496.html
匿名

发表评论

匿名网友

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

确定