Java – Convert String to Float

You can typecast or convert a string to float in Java in many ways. Some of them are using Float.parseFloat(), Float.valueOf(), new Float().

In this tutorial, we shall learn some of the ways of how to convert a string value to a floating value with examples.

String to Float using Float.parseFloat()

Float.parseFloat(str) parses any parsable float value from string to floating point value.

In this example, we shall use Float.parseFloat() method and pass a string that is a parsable float value.

Java Program

/**
 * Java Program - Convert String to Float
 */

public class StringToFloat {

	public static void main(String[] args) {
		//a string
		String str = "1.26354";
		
		//convert string to float
		float f = Float.parseFloat(str);
		
		System.out.print(f);
	}
}

Run the above program and the String is converted to Float.

1.26354

If you do not provide a valid string that is parsable float, Float.parseFloat() throws NumberFormatException.

In the following example program, we shall take a string which does not contain a valid float value.

Java Program

/**
 * Java Program - Convert String to Float
 */

public class StringToFloat {

	public static void main(String[] args) {
		//a string
		String str = "1.abc26354";
		
		//convert string to float
		float f = Float.parseFloat(str);
		
		System.out.print(f);
	}
}

Run the above program and parseFloat() throws NumberFormatException.

Exception in thread "main" java.lang.NumberFormatException: For input string: "1.abc26354"
	at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(Unknown Source)
	at java.base/jdk.internal.math.FloatingDecimal.parseFloat(Unknown Source)
	at java.base/java.lang.Float.parseFloat(Unknown Source)
	at StringToFloat.main(StringToFloat.java:12)

Also, the function Float.parseFloat() throws NullPointerException, if it receives null as argument.

Java Program

/**
 * Java Program - Convert String to Float
 */

public class StringToFloat {

	public static void main(String[] args) {
		//a string
		String str = null;
		
		//convert string to float
		float f = Float.parseFloat(str);
		
		System.out.print(f);
	}
}

Run the above program and parseFloat() throws NullPointerException.

Exception in thread "main" java.lang.NullPointerException
	at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(Unknown Source)
	at java.base/jdk.internal.math.FloatingDecimal.parseFloat(Unknown Source)
	at java.base/java.lang.Float.parseFloat(Unknown Source)
	at StringToFloat.main(StringToFloat.java:12)

As Float.parseFloat() can throw NullPointerException or NumberFormatException, it is recommended to always surround the function parseFloat() with Java Try Catch and handle the exceptions accordingly.

/**
 * Java Program - Convert String to Float
 */

public class StringToFloat {

	public static void main(String[] args) {
		//a string
		String str = "1.2536";
		
		//convert string to float
		float f = 0.0f;
		
		try {
			f = Float.parseFloat(str);
		} catch (NumberFormatException e) {
			System.out.println("Check the string. Not a valid float value.");
		} catch (NullPointerException e) {
			System.out.println("Check the string. String is null.");
		}
		
		System.out.print(f);
	}
}

String to Float using Float.valueOf()

You can also use Float.valueOf() function to convert a string to float.

In the following example, we shall use the method valueOf() to get floating point value from string.

Java Program

/**
 * Java Program - Convert String to Float
 */

public class StringToFloat {

	public static void main(String[] args) {
		//a string
		String str = "-1.6325";
		
		//convert string to float
		float f = 0.0f;
		
		try {
			f = Float.valueOf(str);
		} catch (NumberFormatException e) {
			System.out.println("Check the string. Not a valid float value.");
		} catch (NullPointerException e) {
			System.out.println("Check the string. String is null.");
		}
		
		System.out.print(f);
	}
}

Just like Float.parseFloat(), the function Float.valueOf() also throws NullPointerException if the string argument is null, or a NumberFormatException if the string does not parse to a valid float value.

String to Float using – new Float()

Note: new Float() constructor is depreciated. So, you may get warning when you are following this process. It is recommended to use Float.valueOf() instead of new Float().

You can also use the constructor of Float class, to convert a string to float.

In the following example, we shall use the constructor of Float class to convert from string to float.

Java Program

/**
 * Java Program - Convert String to Float
 */

public class StringToFloat {

	public static void main(String[] args) {

		try {
			Float f = new Float("1.23564");
			System.out.print(f);
		} catch (NumberFormatException e) {
			System.out.println("Check the string. Not a valid float value.");
		} catch (NullPointerException e) {
			System.out.println("Check the string. String is null.");
		}
		
	}
}

Conclusion

In this Java Tutorial, we learned how to Convert a String to Floating point value in Java using Float.parseFloat() and Float.valueOf() methods.