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.