Skip to content

Java convert String to float example

This example shows how to convert string to float in Java using the parseFloat method of the Float wrapper class including the NumberFormatException.

How to convert String to float in Java?

To convert a string to float primitive value, use the parseFloat method of the Float wrapper class.

This method returns a float primitive value by parsing the input string value.

Output

Important Note:

If the floating-point number cannot be parsed from the input string value, the parseFloat method throws a NumberFormatException exception.

Also, if the string is null, the parseFloat method throws a NullPointerException exception.

Output

Here are some of the example string values and their possible outputs.

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

This example is a part of the Java Basic Examples and Java Type conversion Tutorial.

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

About the author

Leave a Reply

Your email address will not be published.