How to Convert String to Float in Java

In this tutorial, we will learn how to convert string to primitive type float or float wrapper class object in Java easily.

There are certain situations where we need to convert a number represented as a string into a float type in Java.

We normally use it when we want to perform mathematical operations on the string which contains a number.

For example, whenever we gain data from JTextField or JComboBox, we receive entered data as a string. If entered data is a float number in string form, we need to convert the string to a float to perform mathematical operations on it.

There are the following ways to convert a string to a primitive type float in Java. They are as:

  • Convert using Float.parseFloat()
  • Convert using Float.valueOf()
  • Converting using new Float(String).floatValue()
  • Convert using DecimalFormat

Convert String to Float in Java using Float.parseFloat()


To convert string to a float, we use Float.parseFloat() method from the Float class. The parseFloat() of Float class is the static utility method.

So, we do not need to create an object of class to call it. We can call it simply using its class name. The general signature of parseFloat() method is as below:

public static float parseFloat(String s)

This method accepts a string containing the float representation to be parsed. It returns the float value.

The parseFloat() method throws an exception named NumberFormatException if the string does not contain a parsable float.


Let’s take a simple example program to convert a string to a float.

Program code 1:

// Java Program to demonstrate the conversion of String into primitive float 
// using Float.parseFloat() method
package javaConversion;
public class StringToFloatConversion {
public static void main(String[] args) 
{
// Create a string literal   
   String str = "50.60";  
// Convert string into float using Float.parseFloat() method. 
   float num = Float.parseFloat(str);  
// Print value of num.  
   System.out.println(num + 50);
   System.out.println(str + 50);
 }
}
Output:
      100.6
      50.6050

Java String to Float Conversion using Float.valueOf()


To convert a string to a float numeric value, we normally use Float.valueOf() method of Java Float class. The general signature of this method is as below:

public static Float valueOf​(String str)

This method converts a string str containing some float number into a Float class object and returns that object holding the value of the specified string.

The valueOf() method throws an exception named NumberFormatException if the string cannot be parsed as a float.


Let’s take a very simple example program to convert String to float class object in Java.

Program code 2:

// Java Program to demonstrate the conversion of String into Float class object.  
// using Float.valueOf() method
package javaConversion;
public class StringToFloatConversion {
public static void main(String[] args) 
{
// Create a string literal   
   String str = "50.60";  
// Convert string into float class object using Float.valueOf() method. 
   Float num = Float.valueOf(str);   
// Print value of num.  
   System.out.println(num + 50);
   System.out.println(str + 50);
 }
}
Output:
     100.6
     50.6050

Converting String into Float Object using new Float(String).floatValue()


Another alternative approach is to create an instance of Float class and then call floatValue() method of Float class. The floatValue() method converts the float object into primitive float type numeric value. This is called “unboxing” in Java.

Unboxing is a process by which we convert an object into its corresponding primitive data type.

Let’s create a Java program to convert String into a float object using floatValue() method.

Program code 3:

// Java Program to demonstrate the conversion of String into float class object.  
// using Float(String).floatValue() method
package javaConversion;
public class StringToFloatConversion {
public static void main(String[] args) 
{
// Create a string literal   
   String str = "100.70";  
// Convert string into float class object using Float(String).floatValue() method. 
   Float num = new Float(str);
   float x = num.floatValue(); 
// Print value of x.  
   System.out.println(x + 50);
   System.out.println(str + 50);
 }
}
Output:
      150.7
      100.7050

Converting String to Float using DecimalFormat


Java provides a class called DecimalFormat that allows to convert a number to its string representation. This class is present in java.text package. We can also use in other way to parse a string into its numerical representation.

Let’s make a Java program for the conversion of a string into float type numeric value using DecimalFormat class.

Program code 4:

// Java Program to demonstrate the conversion of String into float object using DecimalFormat class.
package javaConversion;
import java.text.DecimalFormat;
import java.text.ParseException;
public class StringToFloatConversion {
public static void main(String[] args) 
{
// Create a string literal   
   String str = "200.70";  
// Create an object of DecimalFormat class.    
   DecimalFormat decimalFormat = new DecimalFormat("#");
   try {
     float num = decimalFormat.parse(str).floatValue();
     System.out.println(num + 50);
     System.out.println(str + 50);
   } catch (ParseException e) {
     System.out.println(str + " is not a valid number.");
   }
 }
}
Output:
      250.7
      200.7050

NumberFormatException Case


If we don’t have a number in string literal, invoking Float.parseFloat() or Float.valueOf() methods can throw an exception named NumberFormatException. Let’s take an example program based on it.

Program code 5:

// Java Program to demonstrate the case of NumberFormatException exception.
package javaConversion;
public class StringToFloatConversion {
public static void main(String[] args) 
{
// Create a string literal   
   String str = "Hello";  
// Call parseFloat() method of Float class.     
   Float num = Float.parseFloat(str);  
   System.out.println(num);
 }
}
Output:
      Exception in thread "main" java.lang.NumberFormatException: For input string: "Hello"
	at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
	at java.base/jdk.internal.math.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
	at java.base/java.lang.Float.parseFloat(Float.java:476)
	at javaConversion.StringToFloatConversion.main(StringToFloatConversion.java:9)

In this tutorial, you learned how we can convert a string to float in Java in different ways easily. Hope that you will have understood the basic concept of the conversion of string into float numeric value.
Thanks for reading!!!
Next ⇒ Convert Float to String in Java⇐ Prev Next ⇒

Please share your love