How to Convert String to float in Java

To convert String to float in Java you can use one of the following options-

1- Float.parseFloat(String str) – Returns a new float initialized to the value represented by the specified String.

2- Float.valueOf(String s) – Returns a Float object holding the float value represented by the argument string s.

As you can see parseFloat() method returns a float primitive where as valueOf() method returns a Float object.

Java example to convert String to float using Float.parseFloat


For float numbers you can use “f” or “F” (even d or D which denotes double) so a String like this – “56.45f” won’t result in NumberFormatException while converting. But having any other alphabet like “56.45c” will throw exception.

Java example to convert String to float using Float.valueOf



While converting string to float in Java NumberFormatException is thrown if an invalid number string is passed for conversion.


That’s all for the topic How to Convert String to float in Java. If something is missing or you have something to share about the topic please write a comment.

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.