在使用Java的Selenium Webdriver中,小数位数为2

在使用Java的Selenium Webdriver中,小数位数为2,java,selenium-webdriver,Java,Selenium Webdriver,有人能帮我解决以下问题吗 在文本字段中,我将输入 String input1 = "120.3456"; 但系统将自动取2个小数点并显示“120.35” 现在,我将使用getAttribute(“value”)将数据存储到另一个字符串中 String getValue = driver.findElement(By.xpath("html/....xpath of the text field")).getAttribute("value"); 如何在SeleniumWebDriver中验证

有人能帮我解决以下问题吗

在文本字段中,我将输入

String input1 = "120.3456";
但系统将自动取2个小数点并显示“120.35” 现在,我将使用getAttribute(“value”)将数据存储到另一个字符串中

String getValue = driver.findElement(By.xpath("html/....xpath of the text field")).getAttribute("value");
如何在SeleniumWebDriver中验证给定的输入值是否四舍五入到2个小数点? 如果有人能为我提供最好的方法,我将不胜感激。提前感谢

请尝试以下操作:-

String input1="120.3456";

String getValue = driver.findElement(By.xpath("html/....xpath of the text field")).getAttribute("value");

String str = new BigDecimal(input1).setScale(2, BigDecimal.ROUND_HALF_UP).toString();

return str.equals(getValue) 

希望它能帮上忙……:)

你可以简单地做如下。您需要将
输入
字符串转换为
双精度
。您需要对
输入进行四舍五入
并与
进行比较。如果两个值相等,则返回
true
,否则返回
false

String input1 = "120.3456";
double input1Rounded = Math.round(Double.parseDouble(input1) * 100.0) / 100.0;
String value = driver.findElement(locator).getAttribute("value");
double valueRounded = Double.parseDouble(value);
return (input1Rounded == valueRounded);

不需要。您需要将所有内容转换为十进制基数,例如字符串,并在那里进行舍入或截断。浮点值没有小数位数,因此不能截断或四舍五入。@EJP我不明白你的意思。你能详细说明一下吗?将数字转换为字符串有什么帮助?这个代码可以工作。。。你试过了吗?它也遍布网络,作为如何做到这一点的例子。