-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

1

3 Answers 3

3

You're looking for Float.parseFloat().

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.