Maham
Maham

Reputation: 91

How to convert specific format of string into double

I am using getText() to get a string from the xpath. The string is "500.00 /=" now I want to convert it into double so that I can add it, but the problem is when I am using getText() I am also getting "/=", so how can I exclude this extra string format

//geting amount as string

String actualbookfees_amount = driver.findElement(By.xpath("//[@id='charges']/table[2]/tbody/tr[1]/td[2]")).getText();

String WHT_amount= driver.findElement(By.xpath("//[@id='charges']/table[2]/tbody/tr[2]/td[2]")).getText();

String numPlate_amount = driver.findElement(By.xpath("//[@id='charges']/table[2]/tbody/tr[3]/td[2]")).getText();

String registrationfees_amount= driver.findElement(By.xpath("//[@id='charges']/table[2]/tbody/tr[4]/td[2]")).getText();

String Motor_vehicle=driver.findElement(By.xpath("//*[@id='charges']/table[2]/tbody/tr[5]/td[2]")).getText();

String With_amount=driver.findElement(By.xpath("//*[@id='charges']/table[2]/tbody/tr[5]/td[2]")).getText();

//converting amount in integers
double actualbookfees_amount_int = Double.valueOf(actualbookfees_amount);
double WHT_amount_int = Double.valueOf(WHT_amount);
double numPlate_amount_int = Double.valueOf(numPlate_amount);
double registrationfees_amount_int = Double.valueOf(registrationfees_amount);
double Motor_vehicle_int = Double.valueOf(Motor_vehicle);
double With_amount_int = Double.valueOf(With_amount);

double total_amount_int= actualbookfees_amount_int + WHT_amount_int+numPlate_amount_int+ registrationfees_amount_int+Motor_vehicle_int+With_amount_int;
System.out.println(total_amount_int);

Upvotes: 3

Views: 680

Answers (4)

Shadab Khan
Shadab Khan

Reputation: 1

You can use

Double.parseDouble(String s)

to convert String value into double

Upvotes: 0

Narendra Kekane
Narendra Kekane

Reputation: 86

You can do it by replacing your "," with "".

String actualbookfees_amount = "100,000.00 /=";

actualbookfees_amount = actualbookfees_amount.replace(",", "");
double actualbookfees_amount_int = Double.valueOf(actualbookfees_amount.substring(0, actualbookfees_amount.length()-2));
System.out.println(actualbookfees_amount_int);

Upvotes: 3

brownwood
brownwood

Reputation: 21

I think you should be able to use replaceAll() method from java.lang.String e.g do

String actualbookfees_amount = driver.findElement(By.xpath("//[@id='charges']/table[2]/tbody/tr[1]/td[2]")).getText().replaceAll("/=","");

replaceAll() details: Replaces each substring of this string that matches the given regular expression with the given replacement.

Upvotes: 0

Mirry
Mirry

Reputation: 1

An easy way to do is replacing the '/=' for ''.

You could do for example:

double actualbookfees_amount_int = Double.valueOf(actualbookfees_amount.replace("/=", ""));

Upvotes: 0

Related Questions