I have a string like
> 12.4N-m/kg.
From the above string I need to get a value 12.4
.
When I use replace all function str.replaceAll("[^.0-9]", "")
.
This doesn't work when then string has two dots.
The location of float value may differ.
First discard all non flot characters and then covert to Float like this:
float f = Float.valueOf("> 12.4N-m/kg.".replaceAll("[^\\d.]+|\\.(?!\\d)", ""));
// now f = 12.4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With