-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
  • 6,003
  • 16
  • 51
  • 69
  • 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
  • 868,454
  • 176
  • 1,908
  • 1,964
1

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

User
  • 31,811
  • 40
  • 131
  • 232
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
  • 15,272
  • 18
  • 86
  • 131