-4

Possible Duplicate:
java : convert float to String and String to float

I'm extracting some numbers from a string, these are being stored as another string. Is there a way to convert these strings into a float?

i tried float f = "string"; but this didnt work.

Thanks

Community
  • 1
  • 1
Chris Headleand
  • 5,513
  • 16
  • 48
  • 66
  • http://stackoverflow.com/questions/7552660/java-convert-float-to-string-and-string-to-float – MK. Nov 27 '12 at 21:09

3 Answers3

3

You're looking for Float.parseFloat().

SLaks
  • 837,282
  • 173
  • 1,862
  • 1,933
1

This way---: Float.parseFloat("0.4");

User
  • 31,017
  • 39
  • 128
  • 219
1

Try Float.parseFloat but it does throw a RuntimeException if it fails so this is one time I would recommend catching it.

try {
    Float.parseFloat("0.4")
} catch (NumberFormatException e){ 
  //input is not a float
}

If you want more precision then look at Double.parseDouble() or even the BigDecimal string constructor

RNJ
  • 14,884
  • 18
  • 80
  • 129