How to get float value from string
11,237
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
Author by
user1773765
Updated on June 17, 2022Comments
-
user1773765 almost 2 years
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.