java.lang.NumberFormatException: For input string: "488.15 EUR"

53 views Asked by At

I make automation testing and I want extract text from id element( ok I make it using comand getText() is works, text is "488.15 EUR",after I want make operation with this nr ,I want to subtract 2 from this number that I received:488-2 It is my code :

int suma = Integer.parseInt(Suma.getText());
            
int rest = suma- 2;
Assert.assertTrue(Suma.getText().equals(rest));

But I have next error java.lang.NumberFormatException: For input string: "488.15 EUR" please help me fix this error.Thank you.

1

There are 1 answers

4
Z-100 On BEST ANSWER

First off. "488.15 EUR" is a String, which cannot be parsed to any number, because it doesn't purely consist of numbers.

Secondly, you'll lose those .15 at the end if you try to convert it to an int. Use double instead. it preserves those values.

Remove the last three characters, to make suma.getText() only consist of numbers

String sumaOnlyNum = suma.getText().substring(0, suma.getText().length() - 4);

Convert it to double instead of int

double suma = Double.parseDouble(sumaOnlyNum);