-4

I want to convert GBP 29.15* to just 29.15.

Can anyone please help?

I have already tried parsing to integer/float, substring etc. but getting an error.

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
Yash
  • 5
  • 3
  • You need to [edit] your question to include what you tried and what error you got, – azurefrog Jun 14 '17 at 16:55
  • 3
    Possible duplicate of [Java String remove all non numeric characters](https://stackoverflow.com/questions/10372862/java-string-remove-all-non-numeric-characters) – Jeutnarg Jun 14 '17 at 17:00
  • Use the `subString` method to get the part of the String containing 29.15, and then parse it. `Float.parseFloat("GBP 29.15*".subString(4,9))` – Rapid Readers Jun 14 '17 at 18:03

1 Answers1

1

Try this:

float f = Float.valueOf("GBP 29.15*".replaceAll("[^\\d.]+|\\.(?!\\d)", ""));

It removes all non-number characters and then finds the float value.

See also: How to get float value from string