gpt4 book ai didi

java - 使用 Webdriver 和 Java 运行自动化脚本时出现意外的数字输出

转载 作者:行者123 更新时间:2023-12-02 04:40:34 24 4
gpt4 key购买 nike

在自动化我们的内部应用程序时,遇到了显示 Excel 文件中的数值的问题 - 使用 Selenium Webdriver(使用 Java 和 TestNG 框架)自动化应用程序。

问题:

  • 数值数据在不带后缀“.0”的 xlsx 文件中更新
  • 但是,在控制台中显示数据时,这些数据是显示如下,后缀为“.0”

在xlsx文件中输入如下:

  • 24213
  • 24214
  • 24215
  • 24216

控制台输出:

  • 数据:24213.0
  • 数据:24214.0
  • 数据:24215.0
  • 数据:24216.0

下面是代码片段;请有人告诉一下是什么让代码显示了这个意外的输出。

如果需要任何进一步的详细信息,请告知:

        int row;
row = sheet.getLastRowNum() + 1;
for (int i = 1; i < row; i++)
{

sheet.getRow(i).getCell(1).setCellType(Cell.CELL_TYPE_NUMERIC);

double data = sheet.getRow(i).getCell(1).getNumericCellValue();

System.out.println("Data: " + data);
}

请查找 OneDrive 中共享的输入文件 [ http://1drv.ms/1K7XQqO]

最佳答案

因为您使用的是 double 。

来自JLS 3.10.2. Floating-Point Literals :

A floating-point literal has the following parts: a whole-number part, a decimal or hexadecimal point (represented by an ASCII period character), a fraction part, an exponent, and a type suffix.

double data = sheet.getRow(i).getCell(1).getNumericCellValue();

来自Primitive Data Types

The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values.

使用int

关于java - 使用 Webdriver 和 Java 运行自动化脚本时出现意外的数字输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30237331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com