Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decimal places to 2 values in Selenium Webdriver using Java

Could someone help with the following scenario.

In a textfield, i will give input say

String input1 = "120.3456";

But system will automatically take 2 decimal points and displays "120.35" Now i will store using getAttribute("value") into a different string say

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

How can i validate that my given input value is rounded to 2 decimal points in Selenium Webdriver? Would be grateful if someone can provide me the best approach. Thanks in advance

like image 776
Meghasri Avatar asked Dec 10 '25 06:12

Meghasri


1 Answers

Try as below :-

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) 

Hope it helps...:)

like image 118
Saurabh Gaur Avatar answered Dec 12 '25 19:12

Saurabh Gaur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!