比较 Java 中的两个值

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

Comparing two values in Java

问题

我有两个值我正在尝试比较它们但结果不正确

public void subtotal() throws Exception {
    WebDriverWait wait = new WebDriverWait(session.driver, 100);
    double subtotal_price = 0;
    DecimalFormat decimal = new DecimalFormat("0.00");
    WebElement subtotal = wait.until(ExpectedConditions.visibilityOf(element("Subtotal_cart")));
    Float subtotal_value = Float.parseFloat(subtotal.getText().substring(1));
    logger.info("subtotal_value" + subtotal_value);
    File file = new File("ItemUPC/ItemUPC.txt");
    Scanner sc = new Scanner(file);
    while (sc.hasNextLine()) {
        String[] line = sc.nextLine().split("[|]");
        String price = line[2];
        subtotal_price = subtotal_price + Double.parseDouble(price);
    }
    logger.info("subtotal_price" + subtotal_price);
    if ((subtotal_value) == (subtotal_price)) {
        logger.info("Subtotals updated");
    } else {
        logger.info("Subtotals not updated");
    }
}

以下是ItemUPC文件

2|BATH BENCH|19.00
203|ORANGE BELL|1.78

当我打印subtotal_price和subtotal_value的值时我得到的都是20.78但当在if语句中进行比较时输出为"Subtotals not updated"
不确定我错在哪里有人能帮忙吗谢谢
英文:

I have two values and I am trying to compare them, but getting the worng results:

	public void subtotal() throws Exception {
	WebDriverWait wait = new WebDriverWait(session.driver, 100);
	double subtotal_price = 0;
	DecimalFormat decimal = new DecimalFormat("0.00");
	WebElement subtotal = wait.until(ExpectedConditions.visibilityOf( element("Subtotal_cart")));
	Float subtotal_value = Float.parseFloat(subtotal.getText().substring(1));
	logger.info("subtotal_value"+subtotal_value);
	File file = new File("ItemUPC/ItemUPC.txt");
	Scanner sc = new Scanner(file);
	while (sc.hasNextLine()) {
		String[] line = sc.nextLine().split("[|]");
		String price = line[2];
		subtotal_price = subtotal_price + Double.parseDouble(price);
	}
	logger.info("subtotal_price"+subtotal_price);
	if ((subtotal_value)==(subtotal_price))
	{
		logger.info("Subtotals updated");
	}
	else
	{
		logger.info("Subtotals not updated");
	}
}

The following is the ItemUPC file:

2|BATH BENCH|19.00
203|ORANGE BELL|1.78

When I print the value of subtotal_price and Subtotal_value I am getting both as 20.78, but when its getting compared in the if statement, I am getting output as "Subtotals not updated"
Not sure where I am getting wrong. Can someone please help? Thank you.

答案1

得分: 3

比较浮点数可能会很有挑战性,这是由于浮点类型之间精度的差异,以及它们对十进制数字的二进制表示之间的差异。

你有两个简单的选择:

  1. 将两个值之间的差的绝对值与 epsilon(阈值)值进行比较
  2. 使用 BigDecimal 作为替代你的 Floatdouble 变量类型

示例 1:

// 可能会在某些边缘情况下失败的简化
static final double EPSILON = .001; // 可接受的误差 - 根据需求进行调整
if (Math.abs(subtotal_price - subtotal_value) < EPSILON) {
  logger.info("Subtotals updated");
}
// ...

示例 2:

BigDecimal subtotal_price = new BigDecimal("0");
// ...
BigDecimal subtotal_value = new BigDecimal(subtotal.getText().substring(1));
// ...
if(subtotal_price.compareTo(subtotal_value) == 0) {
  logger.info("Subtotals updated");
}
// ...
    
英文:

Comparing floating point numbers can be challenging, due to differences in precision between floating point types and their binary representations of decimal numbers.

You have two simple options:

  1. Compare the absolute value of the difference between the two values to an epsilon, or threshold, value
  2. Use BigDecimal as a substitute for your Float and double variable types

Example 1:

// simplification that may fail in certain edge cases
static final double EPSILON = .001; // acceptable error - adjust to suit your needs
if (Math.abs(subtotal_price - subtotal_value) &lt; EPSILON) {
  logger.info(&quot;Subtotals updated&quot;);
}
// ...

Example 2:

BigDecimal subtotal_price = new BigDecimal(&quot;0&quot;);
// ...
BigDecimal subtotal_value = new BigDecimal(subtotal.getText().substring(1));
// ...
if(subtotal_price.compareTo(subtotal_value) == 0) {
  logger.info(&quot;Subtotals updated&quot;);
}
// ...
    

huangapple
  • 本文由 发表于 2020年9月1日 03:16:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63676833.html
匿名

发表评论

匿名网友

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

确定