Java Basic Examples

Java convert String to float example

This example shows how to convert String to float in Java. This example also shows how to convert String to float primitive using Float wrapper class.

How to convert String to float in Java?

To convert String to float, use parseFloat method of Java Float wrapper class.

This method returns float primitive value by parsing the input String value.


Important Note:

If the floating point number cannot be parsed from the input String value, parseFloat method throws NumberFormatException.

Also, if the String is null, parseFloat method throws NullPointerException.


Here are some of the example String values and their possible output.

Please note that, “+” (plus sign) or “-“(Minus sign) is allowed as a first character of the String to denote positive and negative value numbers respectively.

Please let us know your views in the comments section below.

Want to learn quickly?
Try one of the many quizzes. I promise you will not be disappointed.


About the author



rahimv has over 15 years of experience in designing and developing Java applications. His areas of expertise are J2EE and eCommerce. If you like the website, follow him on Facebook, Twitter or Google Plus.

Add Comment

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